Maven执行JUnit4测试套件时无限挂起问题求助
Maven执行JUnit测试套件无限挂起排查方案
问题概述
旧项目Maven化改造后,执行mvn test时,caffeine-tests项目的测试套件在运行首个测试时无限挂起,远程Java进程未崩溃但持续等待;直接用JUnit4运行所有测试完全正常。调试确认测试用例已全部加入套件,挂起发生在CaffeineLauncher(TestCase).runBare()栈帧,之后该栈帧被清除。已尝试清理项目、更新Maven项目,问题未解决。
关键信息
远程调试日志
C:\Users\***\Caffeine Tests\src\main\resources>mvnDebug -DforkCount=0 test Listening for transport dt_socket at address: 8000 [INFO] Scanning for projects... [INFO] [INFO] -------------------< caffeine-tests:caffeine-tests >-------------------- [INFO] Building caffeine-tests 0.0.1-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ caffeine-tests --- [INFO] skip non existing resourceDirectory C:\Users\***\Caffeine Tests\src\main\resources [INFO] [INFO] --- compiler:3.13.0:compile (default-compile) @ caffeine-tests --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ caffeine-tests --- [INFO] skip non existing resourceDirectory C:\Users\***\Caffeine Tests\src\main\resources [INFO] [INFO] --- compiler:3.13.0:testCompile (default-testCompile) @ caffeine-tests --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- surefire:3.3.0:test (default-test) @ caffeine-tests --- [WARNING] useSystemClassLoader setting has no effect when not forking [WARNING] The parameter forkCount should likely not be 0. Forking a JVM for tests improves test accuracy. Ensure to have a <forkCount> >= 1. [INFO] Using auto detected provider org.apache.maven.surefire.junit4.JUnit4Provider [INFO] Running caffeine.test.TestCaffeine
TestCaffeine代码
package caffeine.test; import junit.framework.Test; import junit.framework.TestSuite; public final class TestCaffeine extends junit.framework.TestSuite { public TestCaffeine() { super(); } public TestCaffeine(final Class theClass) { super(theClass); } public TestCaffeine(final String name) { super(name); } public static Test suite() { final TestCaffeine suite = new TestCaffeine(); suite.addTest(new TestSuite( caffeine.test.aggregation3.CaffeineLauncher.class)); suite .addTest(new TestSuite(caffeine.test.array.CaffeineLauncher.class)); suite.addTest(new TestSuite( caffeine.test.association1.CaffeineLauncher.class)); suite.addTest(new TestSuite( caffeine.test.association2.CaffeineLauncher.class)); suite.addTest(new TestSuite( caffeine.test.collection.CaffeineLauncher.class)); suite.addTest(new TestSuite( caffeine.test.composition1.CaffeineLauncher.class)); suite.addTest(new TestSuite( caffeine.test.composition2.CaffeineLauncher.class)); suite.addTest(new TestSuite( caffeine.test.composition3.CaffeineLauncher.class)); suite.addTest(new TestSuite( caffeine.test.composition4.CaffeineLauncher.class)); suite.addTest(new TestSuite( caffeine.test.composition5.CaffeineLauncher.class)); suite.addTest(new TestSuite( caffeine.test.composition6.counterexample.CaffeineLauncher.class)); suite.addTest(new TestSuite( caffeine.test.composition7.counterexample.CaffeineLauncher.class)); suite.addTest(new TestSuite( caffeine.test.composition8.counterexample.CaffeineLauncher.class)); suite.addTest(new TestSuite( caffeine.test.composition9.CaffeineLauncher.class)); suite.addTest(new TestSuite( caffeine.test.interactiondiagram1.CaffeineLauncher.class)); suite.addTest(new TestSuite( caffeine.test.interactiondiagram2.CaffeineLauncher.class)); suite.addTest(new TestSuite( caffeine.test.multithreading1.CaffeineLauncher.class)); suite.addTest(new TestSuite( caffeine.test.multithreading2.CaffeineLauncher.class)); suite.addTest(new TestSuite( caffeine.test.nextEvent2.CaffeineLauncher.class)); suite.addTest(new TestSuite( caffeine.test.nextEvent3.CaffeineLauncher.class)); return suite; } }
pom.xml配置
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>net.ptidej</groupId> <artifactId>all-ptidej</artifactId> <version>1.0.0</version> </parent> <groupId>caffeine-tests</groupId> <artifactId>caffeine-tests</artifactId> <version>0.0.1-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.13.0</version> <configuration> <release>21</release> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.3.0</version> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> <dependency> <groupId>net.ptidej.caffeine</groupId> <artifactId>caffeine</artifactId> <version>0.0.1-SNAPSHOT</version> <scope>test</scope> </dependency> </dependencies> </project>
排查与解决步骤
1. 修复SureforkCount配置
日志明确提示forkCount=0不推荐,会影响测试准确性。修改Surefire插件配置,开启fork模式:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.3.0</version> <configuration> <forkCount>1</forkCount> <reuseForks>true</reuseForks> </configuration> </plugin>
执行mvn clean test重新测试。
2. 检查依赖兼容性
- 执行
mvn dependency:tree查看测试依赖树,排查是否存在重复或冲突的JUnit相关依赖,若有则在JUnit依赖中添加<exclusions>排除冲突项。 - 尝试调整Surefire版本至3.2.5,或确认
net.ptidej.caffeine依赖与JDK 21的兼容性。
3. 调试runBare方法
挂起发生在CaffeineLauncher.runBare(),需重点排查:
- 在该方法中添加关键步骤日志(初始化、资源加载、线程操作等),定位挂起点。
- 使用
jstack <进程PID>查看线程栈,检查是否存在死锁、线程等待未唤醒的情况。
4. 切换JUnit 4风格测试套件
当前测试套件为JUnit 3风格,尝试改为JUnit 4注解式:
package caffeine.test; import org.junit.runner.RunWith; import org.junit.runners.Suite; import caffeine.test.aggregation3.CaffeineLauncher; @RunWith(Suite.class) @SuiteClasses({ CaffeineLauncher.class, caffeine.test.array.CaffeineLauncher.class, // 其他测试类依次添加 }) public class TestCaffeine { }
重新执行测试验证。
5. 调整JDK编译版本
旧项目可能基于低版本JDK开发,修改maven-compiler-plugin的<release>配置为对应版本(如8、11),重新编译测试。
6. 清理Maven缓存
执行以下命令彻底清理缓存并重新拉取依赖:
mvn clean install -U
内容的提问来源于stack exchange,提问作者Vishnu Ramesh
相关产品推荐
相关产品推荐

