2

@SelectPackages 和 @SelectClasses 标记没有被 maven 测试命令解析。虽然它在 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,例如添加“包含”标签。


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>

4

4 回答 4

5

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

您的测试代码使用 JUnit 5 @Test 进行注释。因此,此代码必须使用 junit-jupiter-engine 运行。该引擎不支持声明性套件,但会从 maven-surefire-plugin 读取配置。

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

于 2017-02-23T12:49:04.837 回答
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
于 2017-02-23T16:58:17.773 回答
3

@RunWith(JUnitPlatform.class)通过 JUnit 平台运行测试时不支持。如果新的提供者会同时接受UserFeatureSuiteTest,并且PaymentServiceTest您的测试将被执行两次。

用户指南

使用 注释类@RunWith(JUnitPlatform.class)允许它与 IDE 一起运行,并构建支持 JUnit 4 但尚不直接支持 JUnit 平台的系统。

因此,如果您想UserFeatureSuiteTest由 Maven Surefire 运行,您可以使用默认检测到的“旧”JUnit 4 支持,即只需junit-platform-surefire-provider从插件的依赖项中删除。

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

于 2017-02-23T12:26:41.707 回答
1

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

  • Test*
  • *Test
  • *TestCase

您的测试类不遵循这些约定。您应该重命名它或配置 Maven Surefire 插件以对测试类使用另一种模式。

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

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

在这里,您可以看到一个很好的问题,可以概括您的问题,我从中提取了部分答案。你应该看看它。

编辑

在这里你可以看到一个例子来解释你的 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>

正如您在此配置文件中看到的,您指定:

  • 依赖项:您需要 JUnit 的测试范围依赖项才能运行测试
  • 在构建部分,您将添加将运行您的测试及其依赖项的 surefire 插件
于 2017-02-22T13:20:06.137 回答