将 JUnit RunListener 与 Maven 结合使用

发布于 2024-10-17 08:05:12 字数 2081 浏览 0 评论 0原文

我想使用我自己的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青萝楚歌 2024-10-24 08:05:12

我已经尝试解决这个问题三天了,终于发现了我的错误:由于我使用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>

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

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:

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

I should've written:

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

Maybe that was obivous and I just didn't get it before, but this solved my problem.

旧时模样 2024-10-24 08:05:12

确保你的 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.

最冷一天 2024-10-24 08:05:12

作为 JUnit4 的 java doc 主类:

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

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

maven-surefire 插件中的配置侦听器与 TestNG 配合良好

As java doc main class of JUnit4:

JUnitCore is a facade for running tests. It supports running JUnit 4
tests, JUnit 3.8.x tests, and mixtures. To run tests from the command
line, run java org.junit.runner.JUnitCore TestClass1 TestClass2 ....
For one-shot test runs, use the static method runClasses(Class[]). If
you want to add special listeners, create an instance of JUnitCore
first and use it to run the tests.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文