Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

junit

maven

I'm trying to print some data with System.out in my unit tests (@Test mehotds), but it is not showing anything. However, it works properly in @Before method. I'm using JUnit with Maven Surefire plugin.

public class MyTests {

  @Before
  void init(){

    System.out.println("Initializing some data..."); // <- It works.

  }

  @Test
  void shouldRemoveSeries() {

    System.out.println("TEST: Should remove series"); // <- It doesn't.

  }
}

maven-surefire-plugin configuration:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.15</version>
  <configuration>
    <includes>
      <include>**/*Tests.java</include>
    </includes>
  </configuration>
</plugin>

Thanks.

like image 864
Héctor Avatar asked Apr 07 '15 10:04

Héctor


People also ask

How do you test methods in Java?

3.2 Testing Java Classes By ExampleCreate a new source folder called " test " for storing test scripts. Make it a source folder by right-click ⇒ Build Path ⇒ Use as source folder. Run the test and observe the result. Modify some lines to make the test fails and observe the result.


4 Answers

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).

like image 85
Patrick Avatar answered Oct 17 '22 05:10

Patrick


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.

like image 24
khmarbaise Avatar answered Oct 17 '22 05:10

khmarbaise


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;
like image 8
Jordi Castilla Avatar answered Oct 17 '22 05:10

Jordi Castilla


The -Dtest=* command line option of Maven appears to trigger the show of stdout in unit tests.

By convention, the stdout shows in target/surefire-reports/*-output.txt. Apparently, the Surefire plugin developers could not reuse stdout for communication of many things between the tests and the framework.

like image 4
eel ghEEz Avatar answered Oct 17 '22 04:10

eel ghEEz