将JUnit RunListener与Maven一起使用

时间:2011-02-11 11:30:09

标签: junit maven surefire

我想在我的单元测试中使用自己的RunListener。所以我创建了以下类:

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

3 个答案:

答案 0 :(得分:2)

确保您的surefire版本足够新,这只是在2.7版本中引入。

除此之外,如果您在多模块构建的第一个模块中构建 RunListener,那么在构建之前它将无法用于万无一失。非常符合逻辑,但容易忘记。

答案 1 :(得分:2)

我已经尝试解决这个问题3天了,最后发现我的错误:因为我使用surefire-plugin进行报告,所以我已经有了一个报告部分我的pom.xml和我试图在那里指定自定义监听器。相反,我必须在我的pom的构建部分中指定它。基本上,而不是写作:

<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>

我应该写:

<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>

也许这是显而易见的,我之前没有得到它,但这解决了我的问题。

答案 2 :(得分:0)

作为JUnit4的java doc主类:

  

JUnitCore是运行测试的外观。它支持运行JUnit 4   测试,JUnit 3.8.x测试和混合。从命令运行测试   line,运行java org.junit.runner.JUnitCore TestClass1 TestClass2 ....   对于一次性测试运行,请使用静态方法runClasses(Class [])。如果   你想添加特殊的监听器,创建一个JUnitCore的实例   首先使用它来运行测试。

要解决这个问题,我必须编写我的CusomtJUnitCore,添加我的监听器然后通过命令行运行它。

maven-surefire插件中的配置监听器适用于TestNG