有人能告诉我为什么System.out.println()和System.out.print(" \ n")之间存在差异?

时间:2015-11-30 23:43:36

标签: java unit-testing junit

最近我正在做我的软件测试课程。今天,当我对测试代码中的一个函数进行Junit测试时,我遇到了这个问题。   我的测试代码是关于二叉搜索树的,这个单元测试正在测试它的printTree函数。当我创建一个新树并使用该函数将其打印出来时,我想将其转换为String,以便我可以使用assertEquals将输出与所需的值进行比较。

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());
}

和printTree()

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

问题是失败追踪告诉我比较失败了。但是,当我单击打开故障跟踪,所需的值和实际值时,它们完全相同。 唯一的区别在于enter image description here

在我遇到的问题中,有人能告诉我System.out.println()和System.out.print(" \ n")之间的区别是什么?

0 个答案:

没有答案
相关问题