I am learning unit testing with JUnit in Netbeans. But I know how the JUnit works and how to test the System.out.print - JUnit test for System.out.println(). I am a very new to JUnit because I just started JUnit today.
This is my test class
public class CDTest {
CD cd;
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
public CDTest() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
System.setOut(new PrintStream(outContent));
System.setErr(new PrintStream(errContent));
}
@After
public void tearDown() {
System.setOut(null);
System.setErr(null);
}
@Test
public void testSystemOutPrint(){
System.out.print("hello");
System.out.print("hello again");//Please pay attention here. Just System.out.print()
assertEquals("hellohello again", outContent.toString());
}
}
When I run the test, it is working successfully. Now I am only testing to the System.out.print. But when I test System.out.println() as below
@Test
public void testSystemOutPrint(){
System.out.print("hello");
System.out.println("hello again"); //Please pay attention here for println()
assertEquals("hellohello again", outContent.toString());
}
The above code give me this error.
I tried adding system to fix the error like this.
@Test
public void testSystemOutPrint(){
System.out.print("hello");
System.out.println("hello again"); //Please pay attention here for println()
assertEquals("hellohello again ", outContent.toString());//Please pay attention to the space after "again"
}
As you can see above, I added a space after "again". It is still giving me the error when I run. I tried this too
assertEquals("hellohello again\n", outContent.toString());
How can I test System.out.println instead of System.out.print?