문자열 출력에 대한 테스트 코드를 작성하여 사용하던 중
System.out.printf("안녕\n");
System.out.printf("안녕%n");
두가지의 출력문이 눈으로 테스트 하면 동일하지만 코드상 다르다는 것은 알고 있다.
그럼 나는 무엇을 사용하는게 좋을까?
고민이 시작된 testcode는 아래와 같다.
private static final String LINE_SEPARATOR = System.lineSeparator();
private OutputStream captor;
@BeforeEach
void setUp() {
captor = new ByteArrayoutputStream();
System.setOut(new PrintStream(captor);
String value = "안녕";
}
@Test
void escapeNTest() {
System.out.printf("%s\n", value);
assertThat(captor.toString().trim()).contains("안녕" + LINE_SEPARATOR);
}
@Test
void formatNTest() {
System.out.printf("%s%n", value);
assertThat(captor.toString().trim()).contains("안녕" + LINE_SEPARATOR);
}
/**
* Returns the system-dependent line separator string. It always
* returns the same value - the initial value of the {@linkplain
* #getProperty(String) system property} {@code line.separator}.
*
* <p>On UNIX systems, it returns {@code "\n"}; on Microsoft
* Windows systems it returns {@code "\r\n"}.
*
* @return the system-dependent line separator string
* @since 1.7
*/
public static String lineSeparator() {
return lineSeparator;
}