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

弗朗西斯莱尼·坎波斯(Francislainy Campos):

我看到一些人遇到了这个问题,现在已经挣扎了几个星期,但无法在同一项目上同时运行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");
    }
}
tibor17:

我们已对3.0.0-M5版本中的插件进行了改进,因此您无需在依赖项中使用引擎。这种新方法避免了在测试中使用引擎的内部代码,并且使您只能调用API:

也许这个例子文档会有所帮助。

<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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Junit5和Maven:@BeforeAll初始化方法不会被调用

如何使用带的Mockito JUnit5

如何运行使用Maven插件故障保护的JUnit 5集成测试?

究竟选择哪个JUnit5标签使用Maven执行

如何使用JDK10(拼图)和Maven3工作得到JUnit5?

Maven不会运行JUnit5测试

使用FailSafe和SureFire插件并行运行JUnit 4测试

使用Maven surefire与mpirun运行Junit测试

对Spek使用JUnit5标签

JUnit 5:是否可以在Maven Surefire插件中设置TestExecutionListener?

JUnit 5 Surefire Maven:如何为动态Web模块项目运行测试?

Maven Surefire没有运行JUnit 5测试

用junit5隐藏stacktraces的Maven surefire

Maven Surefire Junit测试套件

无法使用Maven执行Junit5测试

JUnit4和JUnit5测试未在IntelliJ中运行

JUnit5 javadocs可供离线使用

Maven surefire插件无法检测Junit5测试

如何配置Maven插件以运行Spock和Junit5测试

如何使用Maven AND JUnit5标签

如何使用Maven运行作为插件安装到Eclipse应用程序的JUnit5测试

尝试使用Maven运行Mockito junit5测试时,java.lang.NoClassDefFoundError:org / junit / platform / commons / PreconditionViolationException

如何使用Jupiter Junit5平台的老式引擎通过gradle配置按类别运行JUnit4测试

为什么Eclipse在应使用JUnit4时尝试使用JUnit5?

使用Selenium Webdriver和junit5进行端到端测试的最佳方法是什么?

使用 Apache Maven Surefire 在 Junit 5 平台上运行基于文件的测试

如何使用 Mockito 进行模拟并使用 Junit5 断言抛出的异常和消息?

使用 mockito 和 Junit5 使用参数模拟静态 void 方法

使用 junit5 声明 List<String[]>