Reputation: 81647
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
Upvotes: 5
Views: 9486
Reputation: 31
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:
<reporting>
<plugins>
[...]
<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>
[...]
</reporting>
I should've written:
<build>
<plugins>
[...]
<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>
[...]
</build>
Maybe that was obivous and I just didn't get it before, but this solved my problem.
Upvotes: 3
Reputation: 503
As java doc main class of JUnit4:
JUnitCore is a facade for running tests. It supports running JUnit 4 tests, JUnit 3.8.x tests, and mixtures. To run tests from the command line, run java org.junit.runner.JUnitCore TestClass1 TestClass2 .... For one-shot test runs, use the static method runClasses(Class[]). If you want to add special listeners, create an instance of JUnitCore first and use it to run the tests.
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
Upvotes: 0
Reputation: 77201
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.
Upvotes: 2