JUnit 5与Surefire Report报NamespacedHierarchicalStore类缺失错误
问题原因及解决方案
问题概述
使用JUnit Jupiter 5.10.1搭配maven-surefire-plugin 3.2.1或3.2.2时,出现NoClassDefFoundError,提示找不到org.junit.platform.engine.support.store.NamespacedHierarchicalStore$CloseAction类,切换到3.1.2版本则正常。
问题原因
maven-surefire-plugin 3.2.1和3.2.2版本默认依赖的JUnit Platform版本低于1.10.1,而JUnit Jupiter 5.10.1依赖的JUnit Platform Engine是1.10.1版本——NamespacedHierarchicalStore$CloseAction类是JUnit Platform 1.10.x版本才新增的,版本不匹配导致类缺失。
解决方案
方案1:升级maven-surefire-plugin到兼容版本
将插件版本升级到3.3.0及以上,这些版本已经适配了JUnit 5.10+系列,无需额外配置:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.3.0</version> </plugin>
方案2:显式指定JUnit Platform Engine版本(不升级插件时用)
如果要保留3.2.1/3.2.2版本,在插件的依赖中添加JUnit Platform Engine的1.10.1版本,确保依赖版本对齐:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.2.2</version> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.10.1</version> </dependency> <!-- 添加以下依赖,对齐JUnit Platform版本 --> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-engine</artifactId> <version>1.10.1</version> </dependency> </dependencies> </plugin>
方案3:回退到maven-surefire-plugin 3.1.2版本
继续使用无异常的旧版本,但不推荐长期使用,因为旧版本可能存在未修复的漏洞或功能缺失:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.1.2</version> </plugin>
内容的提问来源于stack exchange,提问作者bcoveny
相关产品推荐
相关产品推荐

