使用Maven Surefire插件运行JUnit 4和Junit5-2020 [英] Run Both JUnit 4 and Junit5 With Maven Surefire Plugin - 2020

查看:283
本文介绍了使用Maven Surefire插件运行JUnit 4和Junit5-2020的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到一些人遇到了这个问题,现在已经挣扎了几个星期,但是无法在同一项目上同时运行JUnit4和JUnit5(我需要这样做才能维护一些旧测试).我注意到,如果删除maven surefire插件,则可以运行JUnit4测试,而将其添加到POM时,只能运行JUnit5.

I see a few people having this issue and am struggling for a few weeks now, but not able to run both JUnit4 and JUnit5 on the same project (I need this to maintain some old tests). I noticed that if I remove the maven surefire plugin I can run the JUnit4 tests whereas when it's added to the POM only the JUnit5 ones.

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

类似的事情发生在此依赖项上.如果我将其添加到POM文件中,即使存在maven surefire插件,我也可以运行JUnit4测试.但是,我需要将其删除才能运行JUnit5测试.

Similar thing happens to this dependency. If I add it to the POM file I can run JUnit4 tests even if the maven surefire plugin is there. However, I need to remove it to be able to run JUnit5 tests.

<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版本中的插件,因此您无需在依赖项中使用引擎.这种新方法避免了在测试中使用引擎的内部代码,并且使您只能调用API:

We have improved the plugin in the 3.0.0-M5 version so that you do not need to use engines in your dependencies. This new approach avoids using internal code of the engine in your tests and it enables you to only call the API:

也许是示例 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆