As for whether to test, it depends on why you output. I usually test calls to System.out when writing command line utilities because it is a user interface and text output. Itβs not even hard to do - just plain Java code.
System.out. . , "" System.out, . , / .
import static org.junit.Assert.*;
import java.io.*;
import org.junit.*;
public class ConsoleHandlerTest {
private PrintStream originalSysOut;
private ByteArrayOutputStream mockOut;
@Before
public void setSysOut() {
originalSysOut = System.out;
mockOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(mockOut));
}
@After
public void restoreSysOut() {
System.setOut(originalSysOut);
}
@Test
public void outputIsCorrect() {
new ConsoleHandler().write("hello");
assertEquals("message output", "hello".trim(), mockOut.toString().trim());
}
}