Sonarlint를 통한 수정 사항 - leegwichan/java-baseball-playground GitHub Wiki

우선적으로 바꿔야 할 점

  • 항상 커밋 전에 Sonarlint를 확인하여 수정해야 할 부분이 있는지 확인할 것
    • Sonarlint : 항상 코드 품질이 일정 수준을 유지할 수 있게 도와주는 도구
    • 항상 일정 품질을 유지할 수 있도록 특정 주기마다 체크할 것

Printf-style format strings should be used correctly

  • 수정 전
    private static final String INPUT_RETRY_REQUEST =
              String.format("게임을 새로 시작하려면 %s, 종료하려면 %s를 입력하세요.\n", RESTART_INPUT_FORMAT, EXIT_INPUT_FORMAT);
    
  • 수정 후
    private static final String INPUT_RETRY_REQUEST =
              String.format("게임을 새로 시작하려면 %s, 종료하려면 %s를 입력하세요.%n", RESTART_INPUT_FORMAT, EXIT_INPUT_FORMAT);
    
  • 수정 이유
    • Java의 문자열 형식에서는 %n이 플랫폼 독립적인 줄 구분 기호이다.
    • 플랫폼의 줄 구분 기호에 맞게 조정
      • 예를 들어 Windows에서는 "\r\n"으로 표시되는 반면 Unix 계열 시스템(Linux, macOS)에서는 단순히 "\n"으로 표시된다.

Only one method invocation is expected when testing runtime exceptions

  • 수정 전
          @DisplayName("Reader가 null인 경우 예외를 던진다")
          @Test
          void creationTest_whenReaderIsNull_throwException() {
              assertThatThrownBy(() -> InputViewImpl.of(null, new SpyPrinter()))
                      .isInstanceOf(NullPointerException.class);
          }
    
  • 수정 후
          @DisplayName("Reader가 null인 경우 예외를 던진다")
          @Test
          void creationTest_whenReaderIsNull_throwException() {
              Printer printer = new SpyPrinter();
              assertThatThrownBy(() -> InputViewImpl.of(null, printer))
                      .isInstanceOf(NullPointerException.class);
          }
    
  • 수정 이유
    • InputViewImpl.of(null, new SpyPrinter())를 assert 구문으로 테스트 하면, 다음과 같은 경우로 나뉠 수 있다
      • new SpyPrinter()에서 에러가 발생하는 경우
      • InputViewImpl.of(null, printer)에서 에러가 발생하는 경우
    • 따라서 이를 분리하여 assert 에서 예외가 발생하는 부분을 최소화 해야 한다.