使用JUnit5运行ArchUnit测试时未找到测试用例的问题排查
问题原因及解决办法
1. 缺失ArchUnit JUnit5引擎依赖
你只添加了archunit-junit5-api,但JUnit5需要对应的引擎依赖才能识别@ArchTest注解的测试用例。在pom.xml中补充以下依赖:
<dependency> <groupId>com.tngtech.archunit</groupId> <artifactId>archunit-junit5-engine</artifactId> <version>1.1.0</version> <scope>test</scope> </dependency>
2. 确保Maven Surefire插件支持JUnit5
旧版本的Maven Surefire插件不兼容JUnit5,需要升级插件版本到3.0.0及以上。在pom.xml的build节点下添加或更新插件配置:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.2.5</version> </plugin> </plugins> </build>
3. 恢复@ArchTest的原生写法
你的第一个测试类写法是正确的,补充依赖后,IntelliJ和Maven都能识别并运行@ArchTest标注的静态字段测试,无需改用JUnit4的@Test方法。
关于JUnit4@Test方法在Maven中不识别的问题
如果要保留JUnit4风格的测试,需要添加JUnit5对JUnit4的兼容依赖,让Surefire插件能识别JUnit4的测试:
<dependency> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> <version>5.10.0</version> <scope>test</scope> </dependency>
但更推荐统一使用JUnit5 + ArchUnit的原生写法,避免依赖兼容包。
内容的提问来源于stack exchange,提问作者Vimugdh Lall
相关产品推荐
相关产品推荐

