使用PowerMock与Spock时遇到JUnit版本兼容问题
解决Maven构建时JUnit Vintage引擎测试发现失败问题
执行Maven构建时测试无法正常运行,抛出如下警告导致测试未执行:
Mar 07, 2023 8:57:49 AM org.junit.platform.launcher.core.DefaultLauncher handleThrowable WARNING: TestEngine with ID 'junit-vintage' failed to discover tests org.junit.platform.commons.JUnitException: Unsupported version of junit:junit: 4.11. Please upgrade to version 4.12 or later. at org.junit.vintage.engine.JUnit4VersionCheck.checkSupported(JUnit4VersionCheck.java:49) at org.junit.vintage.engine.JUnit4VersionCheck.checkSupported(JUnit4VersionCheck.java:35) at org.junit.vintage.engine.VintageTestEngine.discover(VintageTestEngine.java:63) at org.junit.platform.launcher.core.DefaultLauncher.discoverEngineRoot(DefaultLauncher.java:168) at org.junit.platform.launcher.core.DefaultLauncher.discoverRoot(DefaultLauncher.java:155) at org.junit.platform.launcher.core.DefaultLauncher.discover(DefaultLauncher.java:120) at org.apache.maven.surefire.junitplatform.TestPlanScannerFilter.accept(TestPlanScannerFilter.java:56) at org.apache.maven.surefire.util.DefaultScanResult.applyFilter(DefaultScanResult.java:102) at org.apache.maven.surefire.junitplatform.JUnitPlatformProvider.scanClasspath(JUnitPlatformProvider.java:143) at org.apache.maven.surefire.junitplatform.JUnitPlatformProvider.invoke(JUnitPlatformProvider.java:124) at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:384) at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:345) at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:126) at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:418)
相关POM依赖配置如下:
<dependencies> <dependency> <groupId>org.spockframework</groupId> <artifactId>spock-core</artifactId> <version>1.2-groovy-2.4</version> <scope>test</scope> <exclusions> <exclusion> <artifactId>junit</artifactId> <groupId>junit</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> <scope>test</scope> </dependency> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> <version>2.4.9</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>3.12.1</version> <scope>test</scope> <exclusions> <exclusion> <artifactId>byte-buddy-agent</artifactId> <groupId>net.bytebuddy</groupId> </exclusion> <exclusion> <artifactId>objenesis</artifactId> <groupId>org.objenesis</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito2</artifactId> <version>2.0.9</version> <scope>test</scope> <exclusions> <exclusion> <artifactId>powermock-api-support</artifactId> <groupId>org.powermock</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>2.0.7</version> <scope>test</scope> <exclusions> <exclusion> <artifactId>powermock-module-junit4-common</artifactId> <groupId>org.powermock</groupId> </exclusion> <exclusion> <artifactId>junit</artifactId> <groupId>junit</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-core</artifactId> <version>2.0.7</version> <scope>test</scope> <exclusions> <exclusion> <artifactId>powermock-reflect</artifactId> <groupId>org.powermock</groupId> </exclusion> <exclusion> <artifactId>javassist</artifactId> <groupId>org.javassist</groupId> </exclusion> <exclusion> <artifactId>byte-buddy-agent</artifactId> <groupId>net.bytebuddy</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.jacoco</groupId> <artifactId>org.jacoco.agent</artifactId> <version>0.8.8</version> <scope>test</scope> </dependency> </dependencies>
问题分析
核心原因是项目classpath中存在JUnit 4.11版本,而JUnit Vintage引擎要求最低版本为4.12。虽然你在部分依赖中排除了JUnit,但可能是其他依赖间接引入了4.11版本,同时PowerMock模块版本不一致(2.0.9与2.0.7)也可能加剧依赖冲突问题。
解决方案
定位JUnit 4.11的引入来源:执行以下命令排查依赖树,找到带进来4.11版本的依赖:
mvn dependency:tree -Dincludes=junit:junit根据输出结果,在对应依赖中添加JUnit 4.11的排除规则,或者直接覆盖版本。
统一PowerMock版本:将所有PowerMock相关依赖版本统一为2.0.9,避免版本不一致导致的依赖混乱:
<dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>2.0.9</version> <scope>test</scope> <exclusions> <exclusion> <artifactId>powermock-module-junit4-common</artifactId> <groupId>org.powermock</groupId> </exclusion> <exclusion> <artifactId>junit</artifactId> <groupId>junit</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-core</artifactId> <version>2.0.9</version> <scope>test</scope> <exclusions> <exclusion> <artifactId>powermock-reflect</artifactId> <groupId>org.powermock</groupId> </exclusion> <exclusion> <artifactId>javassist</artifactId> <groupId>org.javassist</groupId> </exclusion> <exclusion> <artifactId>byte-buddy-agent</artifactId> <groupId>net.bytebuddy</groupId> </exclusion> </exclusions> </dependency>显式声明高版本JUnit:在POM中添加JUnit 4.13.2依赖,强制覆盖所有间接引入的低版本:
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency>清理缓存并重新构建:执行以下命令清理本地Maven缓存,确保新依赖生效:
mvn clean install -U
内容的提问来源于stack exchange,提问作者Geogrio
相关产品推荐
相关产品推荐

