junit5 - 通过 Maven 运行测试套件在 junit5 中不起作用

标签 junit5

@SelectPackages 和@SelectClasses 标记未使用 Maven 测试命令进行解析。虽然它在 IDE 中工作正常。即使我尝试在 pom.xml 中使用标签。

这是代码片段:


支付服务测试.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>

最佳答案

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

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

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

关于junit5 - 通过 Maven 运行测试套件在 junit5 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42392512/

相关文章:

传递 lambda 参数时,Kotlin 单元测试数据类中的高阶函数失败

java - Kotlin 中 @BeforeAll 的正确解决方法是什么

java - 可选类型的单元测试用例

java - 如何在 JUnit5 中为测试套件设置自定义测试执行顺序?

java - 如何在构造函数内模拟实例?

java - Mockito 不起作用 MockitoSessionLogger DefFoundError

java - 如何根据 JUnit 中的测试大小对测试进行分类

java - Eclipse NoClassDefFoundError for LauncherFactory 未找到使用 JUnit 5 的测试

templates - 通过 IntelliJ 2019 的 "Create test"功能重新定义用于创建 JUnit 5 类的模板

kotlin - 添加koin测试依赖的Gradle问题