Should we unit test output the console?

I am working with some legacy code that has several commands System.out.printon its own. My eCobertura plugin shows these lines in red, so I want to unit test them.

Here in stackoverflow, I found a way to unit test console outputs that I am very interested in.

Here 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 is green, but when I run the plugin again to cover the code, I get this message:

"Thread-0" java.lang.NullPointerException at net.sourceforge.cobertura.coveragedata.TouchCollector.applyTouchesOnProjectData(TouchCollector.java:186)    net.sourceforge.cobertura.coveragedata.ProjectData.saveGlobalProjectData(ProjectData.java:267)    net.sourceforge.cobertura.coveragedata.SaveTimer.run(SaveTimer.java:31)    java.lang.Thread.run(Thread.java:662)

:

  • unit test System.out.print()'s?
  • eCoberturain ?
  • eCobertura , ?
  • - ?
  • jUnit 4.11, , - ?
  • eCobertura ?
+5
2

unit test System.out.print()?

, . , System.out.print(), , Sun/Oracle . , , , . , , , , .

, : JDBC, /​​, / .

, . , , , . , 100% ( )

Null Pointer

System.setOut(null); System.out null, eCobertura, , - , null. , Out @Before @After, StdOut

eCobertura ?

eCobertura , ?

eCobertura ?

, eclipse eCobertura Standard Out, . , , Standard Out , Cobertura, , , , , .

- ?

, StdOut .

jUnit 4.11, , - ?

,

+6

System.out.println , (unit/integration) .

System test, System Rules .

http://stefanbirkner.github.com/system-rules/

+3

All Articles