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.
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.
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;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With