将JUnit RunListener与Maven一起使用
我想在单元测试中使用我自己的。因此,我创建了以下类:将JUnit RunListener与Maven一起使用,junit,maven,surefire,Junit,Maven,Surefire,我想在单元测试中使用我自己的。因此,我创建了以下类: public class MyRunListener extends RunListener { public MyRunListener() { System.out.println("Creation of Run Listener..."); } @Override public void testStarted(Description description) throws Exce
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>
如您所见,我的RunListener已创建,但在测试执行期间从未调用过
我错过了什么
技术信息:Java 6、Maven 3.0.2、JUnit 4.8.1确保您的surefire版本足够新,这仅在版本2.7中引入
除此之外,如果您在多模块构建的第一个模块中构建RunListener,它将在构建之后才可用于surefire。非常符合逻辑,但很容易忘记。作为JUnit4的java doc主类: JUnitCore是运行测试的门面。它支持运行JUnit4 测试、JUnit 3.8.x测试和混合测试。从命令运行测试 行中,运行java org.junit.runner.JUnitCore TestClass1 TestClass2。。。。 对于一次性测试运行,请使用静态方法runClasses(Class[])。如果 要添加特殊侦听器,请创建JUnitCore的实例 首先,使用它来运行测试 为了解决这个问题,我必须编写CusomtJUnitCore,向其中添加侦听器,然后通过命令行运行它
maven surefire插件中的Config listener与TestNG配合得很好,我已经尝试解决这个问题3天了,最后发现了我的错误:因为我使用surefire插件进行报告,所以我在pom.xml中已经有一个报告部分,并且我试图在那里指定自定义侦听器。相反,我必须在pom的build部分中指定它。因此,基本上,不是写:
<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>
[...]
org.apache.maven.plugins
maven surefire插件
听众
my.company.MyRunListener
[...]
我应该写:
<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>
[...]
org.apache.maven.plugins
maven surefire插件
听众
my.company.MyRunListener
[...]
也许这是obivous,我之前没有得到它,但这解决了我的问题。也许输出会被重定向(比如测试输出)。尝试其他方法,例如
系统。退出(1)
。我也遇到了同样的问题。你找到解决方案了吗?我忘了指出surefire插件的版本,但是在我的测试中,我使用了最新的2.7.1
。
<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>