在 Maven 中使用 JUnit RunListener [英] Using JUnit RunListener with Maven

查看:28
本文介绍了在 Maven 中使用 JUnit RunListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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");
    }

}

现在,在我的 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 时,输出如下:

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

如您所见,我的 RunListener 已创建,但在测试执行期间从未调用过.

So as you can see, my RunListener is created, but never called during tests execution.

我错过了什么?

技术信息:Java 6、Maven 3.0.2、JUnit 4.8.1

Technical information: Java 6, Maven 3.0.2, JUnit 4.8.1

推荐答案

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

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>

我应该写:

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

这篇关于在 Maven 中使用 JUnit RunListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆