我试图写JUnit测试的System.out的,尤其是的System.out.println,但即使通过相关的帖子看完之后,我无法得到的解决方案。
public static void hello(){
System.out.println("Hello");
}
@Test void Test(){
System.setOut(new PrintStream(outContent));
System.setErr(new PrintStream(errContent));
hello();
assertEquals("Hello\n" , outContent.toString());
System.setIn(System.in);
System.setOut(originalOut);
System.setErr(originalErr);
}
它的工作原理完全正常,当我使用打印的println和删除,而不是\ n的的assertEquals,但每当我试图用\ n调用println,测试失败
expected: <Hello
> but was: <Hello
>
org.opentest4j.AssertionFailedError: expected: <Hello
> but was: <Hello
>
甚至错误消息看起来是一样的。有什么办法,我可以使用的println并通过测试?谢谢
这个问题确实是Windows中的不同行分隔符,我更新了你的片段,我换成\n
用+ System.lineSeparator()
本地我的Mac上它的工作原理我希望它是在Windows上也没关系这种变化。
public static void hello(){
System.out.println("Hello");
}
@Test void Test(){
System.setOut(new PrintStream(outContent));
System.setErr(new PrintStream(errContent));
hello();
// Changed the line below
assertEquals("Hello" + System.lineSeparator(), outContent.toString());
System.setIn(System.in);
System.setOut(originalOut);
System.setErr(originalErr);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句