4

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

4

3 回答 3

2

确保您的安全版本足够新,这仅在 2.7 版本中引入。

除此之外,如果您在多模块构建的第一个模块中构建RunListener,则它在构建完成之前将无法使用。很合乎逻辑,但很容易忘记。

于 2011-03-05T15:57:19.787 回答
2

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

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

也许这很明显,我以前没有得到它,但这解决了我的问题。

于 2015-04-10T09:56:17.140 回答
0

作为JUnit4的java doc主类:

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

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

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

于 2014-12-21T04:18:09.563 回答