运行 测试套件无法通过 maven 在 junit5 中运行

running test suite is not working in junit5 through maven

@SelectPackages 和@SelectClasses 标记未通过 Maven 测试进行解析 command.Though 它在 IDE 中工作正常。即使我尝试在 pom.xml 内使用标签。

这是代码片段:


PaymentServiceTest.java

package xyz.howtoprogram.junit5.payment;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

@RunWith(JUnitPlatform.class)
public class PaymentServiceTest {
  @Test
  public void doPaymentZeroAmount() {
    assertEquals(1, 1);
  }
}

UserFeatureSuiteTest.java

@RunWith(JUnitPlatform.class)
@SelectPackages("xyz.howtoprogram.junit5.payment")
public class UserFeatureSuiteTest {
}

它不是运行包下的任何测试用例。虽然它下面有一个测试用例。

xyz.howtoprogram.junit5.payment -> PaymentServiceTest.java

Running xyz.howtoprogram.junit5.suite.UserFeatureSuiteTest
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec - in xyz.howtoprogram.junit5.suite.UserFeatureSuiteTest.

我什至尝试更改 pom.xml,例如添加 'include' 标签。


pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <junit.jupiter.version>5.0.0-M2</junit.jupiter.version>
    <junit.vintage.version>4.12.0-M2</junit.vintage.version>
    <junit.platform.version>1.0.0-M2</junit.platform.version>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <includes>
                    <include>**/UserFeatureSuiteTest.java</include>
                </includes>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>${junit.jupiter.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                    <version>${junit.vintage.version}</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

<dependencies>



    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-runner</artifactId>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
    </dependency>
    <dependency>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
    </dependency>
</dependencies>

默认情况下,Maven 在查找 运行 的测试时使用以下命名约定:

  • Test*
  • *Test
  • *TestCase

您的测试 class 不遵循这些约定。您应该重命名它或 configure Maven Surefire Plugin 以使用另一个模式进行测试 classes.

可能导致您的 Maven 无法工作的另一件事是所有测试都应该位于以下文件夹中:

/my_application/src/test/java/MyFirstExampleTest.java

Here 你可以看到一个很好的问题,可以概括你的问题,我从那里摘取了部分答案。你应该看看它。

编辑

在这里您可以看到一个示例来解释您的 pom.xml 应该如何:

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.codefx.demo</groupId>
    <artifactId>junit-5</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.0.0-M3</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <compilerArgs>
                        <arg>-parameters</arg>
                    </compilerArgs>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19</version>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>1.0.0-M3</version>
                    </dependency>
                    <dependency>
                        <!-- contains the engine that actually runs the Jupiter-tests -->
                        <groupId>org.junit.jupiter</groupId>
                        <artifactId>junit-jupiter-engine</artifactId>
                        <version>5.0.0-M3</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

</project>

As you can see in this configuration file you specify:

  • Dependency: you need a test scoped dependency of JUnit in order to run tests
  • In the build section you will add the surefire plugin that will run your tests along with its dependencies
当通过 JUnit 平台进行 运行ning 测试时,不支持

@RunWith(JUnitPlatform.class)。如果新的提供者同时选择 UserFeatureSuiteTestPaymentServiceTest,您的测试将执行两次。

来自User Guide

Annotating a class with @RunWith(JUnitPlatform.class) allows it to be run with IDEs and build systems that support JUnit 4 but do not yet support the JUnit Platform directly.

因此,如果您希望 UserFeatureSuiteTest 成为 Maven Surefire 的 运行,您可以使用默认检测到的 "old" JUnit 4 支持,即只需删除 junit-platform-surefire-provider 来自插件的依赖项。

或者,您可以直接执行测试,例如PaymentServiceTest,通过 JUnit 平台使用您现有的配置。

抱歉造成混淆...我只是仔细查看了您的代码并意识到您试图混合两个概念。

您的测试代码使用 JUnit 5 @Test 注释。因此这段代码必须是 运行 与 junit-jupiter-engine。该引擎 支持声明式套件,但确实从 maven-surefire-plugin 中读取配置。

@RunWith 是一个 JUnit4 注释,在 junit-jupiter-engine 中被完全忽略。我不认为它会导致测试失败,但它也不会启用任何 JUnit 5 测试。在这种情况下,您放置在套件 class 上的 @SelectPackages 注释只会帮助确定哪些 JUnit 4 测试是 运行 - 但您没有。

下面是最终的 POM,现在可以使用了。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.howtoprogram</groupId>
<artifactId>junit5-test-suite-example</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>


<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <junit.jupiter.version>5.0.0-M2</junit.jupiter.version>
    <junit.vintage.version>4.12.0-M2</junit.vintage.version>
    <junit.platform.version>1.0.0-M2</junit.platform.version>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19</version>
            <configuration>
                <includes>
                    <include>**/*SuiteTest.java</include>
                </includes>
            </configuration>

        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-runner</artifactId>
        <version>${junit.platform.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>${junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

所以输出是:

     T E S T S
-------------------------------------------------------
Feb 23, 2017 10:21:06 PM org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry loadTestEngines
INFO: Discovered TestEngines with IDs: [junit-jupiter]
Running xyz.howtoprogram.junit5.suite.UserFeatureSuiteTest
Feb 23, 2017 10:21:06 PM org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry loadTestEngines
INFO: Discovered TestEngines with IDs: [junit-jupiter]
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.053 sec - in xyz.howtoprogram.junit5.suite.UserFeatureSuiteTest