如何配置Maven同时运行JUnit4、JUnit5及带@RunWith(JUnitPlatform.class)的测试
解决方案
核心问题根源
你遇到的冲突来自于:带@RunWith(JUnitPlatform.class)注解的测试类同时被两个测试引擎识别:
- JUnit Jupiter引擎会识别类内部的JUnit5
@Test注解,尝试原生执行该类测试 - JUnit Vintage引擎会识别类上的JUnit4
@RunWith注解,尝试通过JUnit4运行器执行该类测试
两者冲突导致JUnit Jupiter引擎的正常扫描被阻断,原生JUnit5测试被忽略。
方案1(推荐,无需兼容旧JUnit4运行环境)
如果不需要兼容仅支持JUnit4的旧运行环境(比如老版本IDE、旧构建工具),可以直接删除适配逻辑:
- 去掉
JUnitPlatformClassDemoTest类上的@RunWith(JUnitPlatform.class)注解 - 删除
pom.xml中的junit-platform-runner依赖
修改后三类测试会被自动识别并正常执行。
方案2(需保留@RunWith(JUnitPlatform.class)适配注解)
如果必须保留该适配注解以兼容旧环境,按照以下步骤修改即可:
第一步:调整pom.xml配置
- 统一版本管理并补全依赖scope:
<properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <!-- 统一版本避免冲突 --> <junit5.version>5.7.0</junit5.version> <junit-platform.version>1.7.0</junit-platform.version> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit5.version}</version> <scope>test</scope> </dependency> <!-- 补全vintage引擎的test scope --> <dependency> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> <version>${junit5.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-all</artifactId> <version>1.3</version> <scope>test</scope> </dependency> <dependency> <groupId>org.opentest4j</groupId> <artifactId>opentest4j</artifactId> <version>1.2.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-commons</artifactId> <version>${junit-platform.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit5.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-runner</artifactId> <version>${junit-platform.version}</version> <scope>test</scope> </dependency> </dependencies>
- 调整surefire插件配置,明确启用双引擎:
<plugin> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M5</version> <configuration> <properties> <property> <name>junit-platform.include-engine</name> <value>junit-jupiter,junit-vintage</value> </property> </properties> </configuration> </plugin>
第二步:修改JUnitPlatformClassDemoTest类
给类添加JUnit5的@Disabled注解,让Jupiter引擎跳过该类,仅由Vintage引擎执行:
package example; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.platform.runner.JUnitPlatform; import org.junit.runner.RunWith; @RunWith(JUnitPlatform.class) @Disabled // 仅JUnit Vintage引擎会执行该类,Jupiter引擎直接跳过 public class JUnitPlatformClassDemoTest { @Test void successTest1() { System.out.println("successTest1"); } @Test void successTest2() { System.out.println("successTest2"); } }
验证
修改后执行mvn test,可以看到三类测试都会被执行:
- 原生JUnit4测试
JUnit4Test - 原生JUnit5测试
SampleTest - 适配类
JUnitPlatformClassDemoTest的测试方法
内容的提问来源于stack exchange,提问作者Donald
相关产品推荐
相关产品推荐

