System.out.print() doesn't show anything in test methods

Ran into this as well. I'm using gradle to manage my tasks and I put this in at the end of by build.gradle file :

test {
  testLogging.showStandardStreams = true
}

Now I see System.out.println(whateves).


To get the output of your written Tests via System.out.println you need to configure maven-surefire-plugin to redirect this output into a file which can be achieved by using the following:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.18.1</version>
  <configuration>
    <redirectTestOutputToFile>true</redirectTestOutputToFile>
  </configuration>
</plugin>

The option redirectTestOutputToFile will redirect the output of System.out.println etc. into a file which is separately created:

Excerpt from the docs:

Set this to "true" to redirect the unit test standard output to a file (found in reportsDirectory/testName-output.txt).

Apart from that a System.out.println does not make sense in a unit test in general.


Use Log

private static Logger log = Logger.getLogger(LoggingObject.class);
log.info("I'm starting");

or System.setOut()

private final PrintStream stdout = System.out;
private final ByteArrayOutputStream output = new ByteArrayOutputStream();
private TerminalView terminalview;

Tags:

Java

Junit

Maven