将 JUnit RunListener 与 Maven 结合使用
我想使用我自己的 RunListener< /code>
在我的单元测试中。所以我创建了以下类:
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");
}
}
现在,在我的 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>
现在,当我在项目中运行 mvn test 时,输出如下
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] ------------------------------------------------------------------------
:您可以看到,我的 RunListener 已创建,但在测试执行期间从未被调用。
我错过了什么?
技术信息:Java 6、Maven 3.0.2、JUnit 4.8.1
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我已经尝试解决这个问题三天了,终于发现了我的错误:由于我使用surefire-plugin用于报告目的,所以我的pom.xml中已经有一个报告部分并且我试图在那里指定自定义侦听器。相反,我必须在 pom.xml 的构建部分中指定它。所以基本上,我不应该写:
我应该写:
也许这是显而易见的,我之前只是没有得到它,但这解决了我的问题。
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.
确保你的 Surefire 版本足够新,这只是在 2.7 版本中引入的。
除此之外,如果您在多模块构建的第一个模块中构建 RunListener,则在构建之后才能确保它可用。很有逻辑,但很容易忘记。
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.
作为 JUnit4 的 java doc 主类:
为了解决这个问题,我必须编写我的 CusomtJUnitCore,向其中添加我的侦听器,然后通过命令行运行它。
maven-surefire 插件中的配置侦听器与 TestNG 配合良好
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