lundi 30 novembre 2015

Can someone tell me why there is difference between System.out.println() and System.out.print("\n")?

Recently I am doing the project of my software testing course. Today, when I am doing the Junit test to one of the functions in the test code, I come across this question. My test code is about the binary search tree, this unit test is testing its printTree function. When I create a new tree and use the function to print it out, I want to convert it to String so that I can use assertEquals to compare the output with the desired value.

public class TestofPrintTree {

private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();

@Before
public void setUpStreams() {
    System.setOut(new PrintStream(outContent));
}

@After
public void cleanUpStreams() {
    System.setOut(null);
}

@Test
public void testPrintTree() {

    BinarySearchTree t = new BinarySearchTree( );
    t.printTree( );
    assertEquals("Empty tree\n", outContent.toString());
}

and in the printTree()

    public void printTree( )
{
    if( isEmpty( ) )
        System.out.println( "Empty tree" );
    else
        printTree( root );
}

The problem is that the failure trace told me that the comparison failed. However, when I click to open the failure trace, the desired value and real value, they are exactly the same. The only difference is hereenter image description here

Can someone tell me what is the difference between System.out.println() and System.out.print("\n") in the question I come across?

Aucun commentaire:

Enregistrer un commentaire