I want to use my own RunListener
on my unit tests. So I've created the following class:
public class MyRunListener extends RunListener {
public MyRunListener() {
System.out.println("Creation of Run Listener...");
}
@Override
public void testStarted(Description description) throws Exception {
System.out.println("A Test is going to start");
}
}
Now, in my pom.xml
:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<properties>
<property>
<name>listener</name>
<value>my.company.MyRunListener</value>
</property>
</properties>
</configuration>
</plugin>
Now, when I run mvn test
in my project, the output is the following:
Creation of Run Listener...
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running xxx.SomeNewTests
Test New #1
Test New #2
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.109 sec
Running xxx.SomeErrorTests
Test Old #1
Test Old #2
Tests run: 2, Failures: 2, Errors: 0, Skipped: 0, Time elapsed: 0.125 sec <<< FAILURE!
Results :
Failed tests:
testOldOne(xxx.SomeErrorTests)
testOldTwo(xxx.SomeErrorTests)
Tests run: 4, Failures: 2, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
So as you can see, my RunListener is created, but never called during tests execution.
What did I missed?
Technical information: Java 6, Maven 3.0.2, JUnit 4.8.1
As java doc main class of JUnit4:
To solve the problem, I had to write my CusomtJUnitCore, add my listener to it then run it by command line.
Config listener in maven-surefire plugin works well with TestNG
I've been trying to solve this for 3 days now and finally found my mistake: Since I was using the surefire-plugin for reporting purposes, i already had a reporting-section inside my pom.xml and I was trying to specify the custom listener there. Instead I had to specify it inside the build-section of my pom. So basically, instead of writing:
I should've written:
Maybe that was obivous and I just didn't get it before, but this solved my problem.
Make sure your surefire version is new enough, this was only introduced in version 2.7.
Other than that, if you build the RunListener in the first module of a multimodule build, it will not be available for surefire until after it is built. Pretty logical, but easy to forget.