关于junit5:Maven和JUnit 5:在一个类中运行一个测试

Maven and JUnit 5: run a single test in a class

我一直在尝试使用Maven(版本3.3.9)和JUnit 5(NOT 4)在一个类中使用以下命令运行单个测试失败:

1
mvn -Dtest=EmitRulesTest#cr_filter_contact_points_for_C4C_output test

此命令执行所有测试。

尝试这个命令实际上执行了类中的所有测试:

1
mvn test -Dtest=EmitRulesTest

这是我的JUnit 5 Maven配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<dependencies>
    ...
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        junit-jupiter-params</artifactId>
        <version>5.0.0-RC2</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        ...    
        <plugin>
            maven-surefire-plugin</artifactId>
            <version>2.19</version>
            <configuration>
                <systemPropertiesFile>${basedir}/src/test/resources/definitions/system.properties</systemPropertiesFile>
                <includes>
                    <include>**/*Test.java</include>
                </includes>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    junit-platform-surefire-provider</artifactId>
                    <version>1.0.0-RC2</version>
                </dependency>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    junit-jupiter-engine</artifactId>
                    <version>5.0.0-RC2</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

进一步参考:使用Maven运行单个测试


您可以使用以下格式:

1
mvn test -Dtest=TestClass#testMethod

您可以在此处找到更多信息:http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html

这是我的POM.xml的摘录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<dependency>
    <groupId>org.junit.jupiter</groupId>
    junit-jupiter-api</artifactId>
    <version>${junit.jupiter.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>junit</groupId>
    junit</artifactId>
    <version>${junit.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    junit-jupiter-engine</artifactId>
    <version>${junit.jupiter.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.vintage</groupId>
    junit-vintage-engine</artifactId>
    <version>${junit.vintage.version}</version>
    <scope>test</scope>
</dependency>

...

<properties>
    <junit.version>4.12</junit.version>
    <junit.jupiter.version>5.1.0</junit.jupiter.version>
    <junit.vintage.version>5.1.0</junit.vintage.version>
    <junit.platform.version>1.1.0</junit.platform.version>
</properties>