【问题标题】:Using JUnit RunListener with Maven在 Maven 中使用 JUnit RunListener
【发布时间】:2011-06-25 12:07:10
【问题描述】:

我想在我的单元测试中使用我自己的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

【问题讨论】:

  • 也许输出被重定向(如测试输出)。试试别的,例如System.exit(1).
  • 我遇到了同样的问题。你找到解决办法了吗?

标签: junit maven surefire


【解决方案1】:

我已经尝试解决这个问题 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>

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

【讨论】:

    【解决方案2】:

    作为JUnit4的java doc主类:

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

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

    maven-surefire 插件中的配置监听器与 TestNG 配合得很好

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • 我忘了指明surefire插件的版本,但在我的测试中,我使用了最新的2.7.1
      猜你喜欢
      • 2012-12-25
      • 2012-05-19
      • 1970-01-01
      • 2022-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多