Testing System.out.println Outputs

Although System.out.println invocations might be buried deep inside static methods:

public class App {

    public final static void main(String args[]) {
        if (args.length != 2) {

    static void usage() {

        System.out.println("Use: java -jar airfield.App (...)");


...they are actually very easy to test by mocking-out the PrintStream:

import java.io.PrintStream;
import org.junit.Test;
import static org.mockito.Matchers.startsWith;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

public class AppTest {

    public void mainUsage() {
        PrintStream out = mock(PrintStream.class);
        App.main(new String[]{});

The example above was extracted from the: airfield and loadr projects.

See you at the Java 8 and Java EE 7 Testing andn Quality workshop at MUC Airport.


You can also use PowerMock to test static methods.

Posted by Javin on October 18, 2015 at 05:16 AM CEST #

If the output is produced by multiple println's, you could also use the SystemOutRule.

Posted by Roland on October 19, 2015 at 12:09 PM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
...the last 150 posts
...the last 10 comments