[英]JUnit test for System.out.println/System.err.println
I have to write tests for an application and Im trying to write a juint test for somthing that is printed on the console. 我必须为应用程序编写测试,而林正试图为打印在控制台上的东西编写虚拟测试。 The application has --> 该应用程序具有->
System.err.println("Username cannot be empty");
This is what I have done so far : 到目前为止,这是我所做的:
ByteArrayOutputStream errContent = new ByteArrayOutputStream();
System.setErr(new PrintStream(errContent));
//left username empty and pressed login
assertEquals("Username cannot be empty", errContent.toString());
But I get ComparisonFailure
但是我得到了ComparisonFailure
expected <Username cannot be empty[]>
but was <Username cannot be empty[
]>
//difference of next line.
Anyone know how to solve this ? 有人知道如何解决这个问题吗? Thanks. 谢谢。
EDIT : Tried assertEquals("Username cannot be empty\\n", errContent.toString());
编辑:尝试assertEquals("Username cannot be empty\\n", errContent.toString());
Now I get : 现在我得到:
expected <Username cannot be empty[]
>
but was <Username cannot be empty[
]
>
What you are missing in your test is, that System.err.println()
has a linefeed at the end. 您在测试中缺少的是, System.err.println()
在末尾有换行符。 Change assertEquals("Username cannot be empty", errContent.toString());
更改assertEquals("Username cannot be empty", errContent.toString());
to assertEquals("Username cannot be empty"+System.getProperty("line.separator"), errContent.toString());
到assertEquals("Username cannot be empty"+System.getProperty("line.separator"), errContent.toString());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.