简体   繁体   中英

Testing System.out.println not working but System,out.print using JUnit

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?

Try this

String expected = "hellohello again " + System.getProperty("line.separator");
assertEquals(expected, outContent.toString());

System.out.println prints a new line. Change: "hellohello again" to

"hellohello again" + System.lineSeparator()

I haven't run this, but I suspect System.out.println is adding a linebreak to the end of "hellohello again" . So your strict equals is probably failing for that.

You may want to consider either changing your assertion to

assertTrue(outContent.toString().startsWith("hellohello again"));

Or add a line break to the end of your expected String. That might be tricky because it will change depending on the system running your tests. So you might need something like System.getProperty("line.separator"); or a similar solution to getting the correct line break character at runtime.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM