Reputation: 4589
While executing tests in Maven Surefire I see ClassNotFoundException
s from time to time.
This really gives me a headache, since:
CNFE
is raised I had a look at the class path (during runtime!) and it looks fine!I took the code of the "class path scanner" from 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:
The latter jar bundles all my Maven dependencies in its Manifest file, as described in the Surefire docs.
So I dug further and analysed the "Class-Path" attribute of the manifest. There I found the dependent jar listed, where the missing class should have come from. When browsing through the jar's entries, I also found the missing class there. The fully qualified path also matches.
So in principle everything seems to be correct and in place. Where should I continue to investigate now?
Upvotes: 1
Views: 154
Reputation: 14951
There are several things to check for problems like these.
spring.jar
vs. spring-core.jar
. The old Tattletale plugin might be useful to get started.If using JUnit, make sure Maven surefire is using the right JUnit provider for your version of JUnit. Run the build in debug mode with -X
(redirect output to a file if using command line). Grep the output for surefire-junit
. You should find something like this:
[DEBUG] org.apache.maven.surefire:surefire-junit4:jar:2.16:test (selected for test)
Now, make sure the version of the provider matches the version of JUnit used. Check out the Maven docs for information on which provider to use and how to configure.
Upvotes: 1