Skip to content

Maven Failsafe fails with java.lang.NoClassDefFoundError

I’ve started a new project: PostfixSQLConfig. It’s a simple Spring Boot application that is essentialy supposed to provide CRUD access for 4 simple databse tables. I wrote the the repository for the first table and some basic integration tests for said repository. Since this particular table should not provide update functionality, I implemented update function as:

public void update(@NonNull Domain domain) throws NotUpdatableException {
    throw new NotUpdatableException("Domain entities are read-only");

where NotUpdatableException is my custom exception class.

The IT for this code look like this:

@Test(expected = NotUpdatableException.class)
public void testUpdate() throws NotUpdatableException {
    val domain = Domain.of("test");


If run this test from my IDE (IntelliJ 2018.2 EAP) it passes fine, but running mvn verify fails with:

java.lang.NoClassDefFoundError: com/github/forinil/psc/exception/NotUpdatableException
  at java.lang.Class.getDeclaredMethods0(Native Method)
  at java.lang.Class.privateGetDeclaredMethods(
  at java.lang.Class.privateGetMethodRecursive(
  at java.lang.Class.getMethod0(
  at java.lang.Class.getMethod(
  at org.apache.maven.surefire.util.ReflectionUtils.tryGetMethod(
  at org.apache.maven.surefire.common.junit3.JUnit3TestChecker.isSuiteOnly(
  at org.apache.maven.surefire.common.junit3.JUnit3TestChecker.isValidJUnit3Test(
  at org.apache.maven.surefire.common.junit3.JUnit3TestChecker.accept(
  at org.apache.maven.surefire.common.junit4.JUnit4TestChecker.accept(
  at org.apache.maven.surefire.util.DefaultScanResult.applyFilter(
  at org.apache.maven.surefire.junit4.JUnit4Provider.scanClassPath(
  at org.apache.maven.surefire.junit4.JUnit4Provider.setTestsToRun(
  at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(
  at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(
  at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(
  at org.apache.maven.surefire.booter.ForkedBooter.execute(
  at org.apache.maven.surefire.booter.ForkedBooter.main(
Caused by: java.lang.ClassNotFoundException: 
  at java.lang.ClassLoader.loadClass(
  at sun.misc.Launcher$AppClassLoader.loadClass(
  at java.lang.ClassLoader.loadClass(
  ... 18 more

And I have honestly no idea why…

Has someone ever encountered this problem?



I figured it out, so I’m answering my own quesiton in case someone else has the same problem.

It turns out that maven-failsafe-plugin does not add target/classes directory to the classpath, but rather the resulting jar, which works fine in most cases.

When it comes to Spring Boot, however, the resulting jar contains Spring Boot custom classloader classes in place of contents of target/classes directory, which are moved to directory BOOT-INF/classes. Since maven-failsafe-plugin uses ‘regular’ classloader it only loads Spring Boot classloader classes, failing in the first place it is expected to use one of the project classes.

To run IT tests in Spring Boot project, one has to exclude the packaged jar from dependencies and add either the original, unmodified jar or target/classes directory, which is what I did.

The correct configuration for maven-failsafe-plugin and Spring Boot is:

User contributions licensed under: CC BY-SA
7 People found this is helpful