I am working with some legacy code that has some System.out.print
commands in itself.
My eCobertura plugin shows this lines red, so I want to unit test them.
Here in stackoverflow I found a way to unit test console outputs which i thing is very interesting.
This is how I do it:
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());
}
So far so good, the test goes green but when I run the code coverage plugin again, I get this message:
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)
I have some doubts:
- Is it correct to try to unit test
System.out.print()'s
? - Is eCoberturaincompatible with this type of test?
- If eCobertura is not compatible with this type of test, why does it show the line red?
- Is there something wrong in my test?
- I am using jUnit 4.11 do you think this has something to do with it?
- why is eCobertura giving me this error?
eCobertura
tells me that the line is covered. Why is that exception being fired? – Englishism