[英]ClassNotFoundExceptions in Maven Surefire tests
While executing tests in Maven Surefire I see ClassNotFoundException
s from time to time.在 Maven Surefire 中执行测试时,我不时看到ClassNotFoundException
。
This really gives me a headache, since:这真的让我很头疼,因为:
CNFE
is raised I had a look at the class path (during runtime!) and it looks fine!当提出CNFE
,我查看了类路径(在运行时!),看起来不错!I took the code of the "class path scanner" from Arno Haase:我从 Arno Haase 那里拿到了“类路径扫描器”的代码:
public List<URL> getRootUrls () {
List<URL> result = new ArrayList<> ();
ClassLoader cl = Thread.currentThread().getContextClassLoader();
while (cl != null) {
if (cl instanceof URLClassLoader) {
URL[] urls = ((URLClassLoader) cl).getURLs();
result.addAll (Arrays.asList (urls));
}
cl = cl.getParent();
}
return result;
}
The list of URLs is quite short: URL 列表很短:
The latter jar bundles all my Maven dependencies in its Manifest file, as described in the Surefire docs.后一个 jar 将我所有的 Maven 依赖项捆绑在其 Manifest 文件中, 如 Surefire 文档中所述。
So I dug further and analysed the "Class-Path" attribute of the manifest.所以我进一步挖掘并分析了清单的“Class-Path”属性。 There I found the dependent jar listed, where the missing class should have come from.在那里我找到了列出的依赖 jar,缺少的类应该来自哪里。 When browsing through the jar's entries, I also found the missing class there.在浏览 jar 的条目时,我还在那里找到了丢失的类。 The fully qualified path also matches.完全限定的路径也匹配。
So in principle everything seems to be correct and in place.所以原则上一切似乎都是正确和到位的。 Where should I continue to investigate now?我现在应该在哪里继续调查?
There are several things to check for problems like these.有几件事可以检查这些问题。
spring.jar
vs. spring-core.jar
.请特别注意组 ID 或工件 ID 已更改的工件,例如spring.jar
与spring-core.jar
。 The old Tattletale plugin might be useful to get started.旧的Tattletale 插件可能对开始有用。 If using JUnit, make sure Maven surefire is using the right JUnit provider for your version of JUnit.如果使用 JUnit,请确保 Maven surefire 为您的 JUnit 版本使用正确的 JUnit 提供程序。 Run the build in debug mode with -X
(redirect output to a file if using command line).使用-X
在调试模式下运行构建(如果使用命令行,则将输出重定向到文件)。 Grep the output for surefire-junit
. Grep 输出surefire-junit
。 You should find something like this:你应该找到这样的东西:
[DEBUG] org.apache.maven.surefire:surefire-junit4:jar:2.16:test (selected for test) [DEBUG] org.apache.maven.surefire:surefire-junit4:jar:2.16:test(选择测试)
Now, make sure the version of the provider matches the version of JUnit used.现在,确保提供程序的版本与使用的 JUnit 版本匹配。 Check out the Maven docs for information on which provider to use and how to configure.查看Maven 文档以获取有关使用哪个提供程序以及如何配置的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.