我正在使用一些遗留代码本身有一些System.out.print命令.
我的eCobertura插件显示这条线为红色,所以我想对它们进行单元测试.
Here在stackoverflow中我发现了一种单元测试控制台输出的方法,我觉得非常有趣.
我是这样做的:
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 out() {
System.out.print("Some message from the system");
assertEquals("Some message from the system", outContent.toString());
}
到目前为止,测试变得很好,但是当我再次运行代码覆盖率插件时,我收到以下消息:
Exception in thread “Thread-0” java.lang.NullPointerException at
net.sourceforge.cobertura.coveragedata.TouchCollector.applyTouchesOnProjectData(TouchCollector.java:186)
at
net.sourceforge.cobertura.coveragedata.ProjectData.saveGlobalProjectData(ProjectData.java:267)
at
net.sourceforge.cobertura.coveragedata.SaveTimer.run(SaveTimer.java:31)
at java.lang.Thread.run(Thread.java:662)
我有些疑惑:
>尝试单元测试System.out.print()是否正确?
> eCobertura是否与此类测试兼容?
>如果eCobertura与此类测试不兼容,为什么它显示红色线?
>我的测试中有什么问题吗?
>我使用的是jUnit 4.11你认为这与它有关吗?
>为什么eCobertura会给我这个错误?