Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit test for System.out.println()

I need to write JUnit tests for an old application that's poorly designed and is writing a lot of error messages to standard output. When the getResponse(String request) method behaves correctly it returns a XML response:

@BeforeClass public static void setUpClass() throws Exception {     Properties queries = loadPropertiesFile("requests.properties");     Properties responses = loadPropertiesFile("responses.properties");     instance = new ResponseGenerator(queries, responses); }  @Test public void testGetResponse() {     String request = "<some>request</some>";     String expResult = "<some>response</some>";     String result = instance.getResponse(request);     assertEquals(expResult, result); } 

But when it gets malformed XML or does not understand the request it returns null and writes some stuff to standard output.

Is there any way to assert console output in JUnit? To catch cases like:

System.out.println("match found: " + strExpr); System.out.println("xml not well formed: " + e.getMessage()); 
like image 536
Mike Minicki Avatar asked Jul 13 '09 13:07

Mike Minicki


People also ask

Can we use system out Println in JUnit?

println and System. out. print will usually print the output String to the screen, however with the below code, you could actually redirect the print out to a Stream and thereafter use it for JUnit testing. PrintStream originalOut = System.

Can JUnit be used for system testing?

JUnit can be used as a test runner for any kind of test: e.g. system and integration tests; tests which are interacting with a deployed application.

What is a JUnit test?

JUnit is a unit testing open-source framework for the Java programming language. Java Developers use this framework to write and execute automated tests. In Java, there are test cases that have to be re-executed every time a new code is added. This is done to make sure that nothing in the code is broken.


2 Answers

using ByteArrayOutputStream and System.setXXX is simple:

private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); private final PrintStream originalOut = System.out; private final PrintStream originalErr = System.err;  @Before public void setUpStreams() {     System.setOut(new PrintStream(outContent));     System.setErr(new PrintStream(errContent)); }  @After public void restoreStreams() {     System.setOut(originalOut);     System.setErr(originalErr); } 

sample test cases:

@Test public void out() {     System.out.print("hello");     assertEquals("hello", outContent.toString()); }  @Test public void err() {     System.err.print("hello again");     assertEquals("hello again", errContent.toString()); } 

I used this code to test the command line option (asserting that -version outputs the version string, etc etc)

Edit: Prior versions of this answer called System.setOut(null) after the tests; This is the cause of NullPointerExceptions commenters refer to.

like image 169
dfa Avatar answered Sep 19 '22 17:09

dfa


I know this is an old thread, but there is a nice library to do this: System Rules
Example from the docs:

public void MyTest {     @Rule     public final SystemOutRule systemOutRule = new SystemOutRule().enableLog();      @Test     public void overrideProperty() {         System.out.print("hello world");         assertEquals("hello world", systemOutRule.getLog());     } } 

It will also allow you to trap System.exit(-1) and other things that a command line tool would need to be tested for.

like image 30
Will Avatar answered Sep 23 '22 17:09

Will