System.out.println/System.err.println 的 JUnit 测试
JUnit test for System.out.println/System.err.println
我必须为应用程序编写测试,我正在尝试为打印在控制台上的东西编写联合测试。
该应用程序有 -->
System.err.println("Username cannot be empty");
这是我到目前为止所做的:
ByteArrayOutputStream errContent = new ByteArrayOutputStream();
System.setErr(new PrintStream(errContent));
//left username empty and pressed login
assertEquals("Username cannot be empty", errContent.toString());
但是我得到了ComparisonFailure
expected <Username cannot be empty[]>
but was <Username cannot be empty[
]>
//difference of next line.
有人知道怎么解决吗?
谢谢。
编辑:已尝试 assertEquals("Username cannot be empty\n", errContent.toString());
现在我得到:
expected <Username cannot be empty[]
>
but was <Username cannot be empty[
]
>
您在测试中遗漏的是 System.err.println()
最后有一个换行符。将 assertEquals("Username cannot be empty", errContent.toString());
更改为 assertEquals("Username cannot be empty"+System.getProperty("line.separator"), errContent.toString());
我必须为应用程序编写测试,我正在尝试为打印在控制台上的东西编写联合测试。 该应用程序有 -->
System.err.println("Username cannot be empty");
这是我到目前为止所做的:
ByteArrayOutputStream errContent = new ByteArrayOutputStream();
System.setErr(new PrintStream(errContent));
//left username empty and pressed login
assertEquals("Username cannot be empty", errContent.toString());
但是我得到了ComparisonFailure
expected <Username cannot be empty[]>
but was <Username cannot be empty[
]>
//difference of next line.
有人知道怎么解决吗? 谢谢。
编辑:已尝试 assertEquals("Username cannot be empty\n", errContent.toString());
现在我得到:
expected <Username cannot be empty[]
>
but was <Username cannot be empty[
]
>
您在测试中遗漏的是 System.err.println()
最后有一个换行符。将 assertEquals("Username cannot be empty", errContent.toString());
更改为 assertEquals("Username cannot be empty"+System.getProperty("line.separator"), errContent.toString());