java - 使用 Maven Surefire 插件运行 JUnit 4 和 Junit5 - 2020

标签 java maven junit junit4 junit5

我看到一些人遇到这个问题并且现在已经挣扎了几个星期,但无法在同一个项目上同时运行 JUnit4 和 JUnit5(我需要这个来维护一些旧测试)。我注意到如果我删除 maven surefire 插件,我可以运行 JUnit4 测试,而当它添加到 POM 时,只有 JUnit5 测试。

<plugins>
    <plugin>
       <artifactId>maven-surefire-plugin</artifactId>
       <version>3.0.0-M4</version>
    </plugin>
</plugins>
这种依赖也会发生类似的事情。如果我将它添加到 POM 文件中,即使 maven surefire 插件在那里,我也可以运行 JUnit4 测试。但是,我需要删除它才能运行 JUnit5 测试。
<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <version>${junit5.version}</version>
    <scope>test</scope>
</dependency>
这是我完整的pom
<?xml version="1.0" encoding="UTF-8"?>
<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.hmhco</groupId>
<artifactId>tests</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
    <java.version>1.8</java.version>
    <maven.compiler.version>3.8.1</maven.compiler.version>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <rest-assured.version>3.0.0</rest-assured.version>
    <json-schema-validator.version>3.3.0</json-schema-validator.version>
    <junit5.version>5.2.0</junit5.version>
</properties>

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
        <version>${junit5.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.5.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jaxb</groupId>
        <artifactId>jaxb-runtime</artifactId>
        <version>2.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.30</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M4</version>
        </plugin>
    </plugins>
</build>

这些是我尝试使用 mvn test 运行的小类
import org.junit.Test;

public class J4Test {

@Test
public void testing() {
    System.out.println("Testing J4");
    }
}
——
import org.junit.jupiter.api.Test;

public class J5Test {

@Test
public void testing() {
    System.out.println("Testing J5");
    }
}

最佳答案

我们改进了 3.0.0-M5 version 中的插件这样您就不需要在依赖项中使用引擎。这种新方法避免在您的测试中使用引擎的内部代码,它使您能够只调用 API:
也许这个 exampledocumentation有帮助。

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

关于java - 使用 Maven Surefire 插件运行 JUnit 4 和 Junit5 - 2020,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62552652/

相关文章:

java - 一起共享图像和文本不适用于 Android 上的 Telegram

java - 自定义共享意向对话框这可能吗?

java - Oracle Java API 中的 NO_COPY 指令

java - Linux 上的 Protocol Buffer 符号查找错误

java - 使用 TestSuite 时,我可以避免在 Eclipse 中运行两次 junit 测试吗?

java - 生成包含当前日期和计数器的 id

java - 使用 Karaf 的 features-maven-plugin maven 插件需要帮助... osgi

java - Maven Failsafe 插件找不到任何测试用例

java - 我需要在测试前在 junit 中运行一次初始化代码 - java

java - 在内部使用私有(private)支持方法对公共(public)方法进行单元测试?