Fra*_*pos 11 java junit junit4 maven junit5
我看到一些人遇到了这个问题,并且已经挣扎了几个星期,但无法在同一个项目上运行 JUnit4 和 JUnit5(我需要它来维护一些旧的测试)。我注意到,如果删除 Maven Surefire 插件,我可以运行 JUnit4 测试,而当它添加到 POM 时,只能运行 JUnit5 测试。
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
</plugin>
</plugins>
Run Code Online (Sandbox Code Playgroud)
这种依赖关系也会发生类似的情况。如果我将它添加到 POM 文件中,即使有 Maven Surefire 插件,我也可以运行 JUnit4 测试。但是,我需要删除它才能运行 JUnit5 测试。
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit5.version}</version>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
这是我完整的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>
Run Code Online (Sandbox Code Playgroud)
这些是我尝试使用的小类mvn test
import org.junit.Test;
public class J4Test {
@Test
public void testing() {
System.out.println("Testing J4");
}
}
Run Code Online (Sandbox Code Playgroud)
--
import org.junit.jupiter.api.Test;
public class J5Test {
@Test
public void testing() {
System.out.println("Testing J5");
}
}
Run Code Online (Sandbox Code Playgroud)
就我而言,将 maven-surefire-plugin 升级到版本 3.0.0-M5 没有帮助,但添加引擎有帮助。现在我的 pom.xml 中有两个引擎作为依赖项:
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
第一个是运行 JUnit 4 测试,第二个是运行 JUnit 5 测试。所以就我而言,两者现在一起运行。
我们改进了插件,以便3.0.0-M5 version您不需要在依赖项中使用引擎。这种新方法避免在测试中使用引擎的内部代码,并且使您只能调用 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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9740 次 |
| 最近记录: |