16

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:

@Override
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");

    domainRepository.update(domain);
}

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(Class.java:2701)
  at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
  at java.lang.Class.getMethod0(Class.java:3018)
  at java.lang.Class.getMethod(Class.java:1784)
  at org.apache.maven.surefire.util.ReflectionUtils.tryGetMethod(ReflectionUtils.java:60)
  at org.apache.maven.surefire.common.junit3.JUnit3TestChecker.isSuiteOnly(JUnit3TestChecker.java:65)
  at org.apache.maven.surefire.common.junit3.JUnit3TestChecker.isValidJUnit3Test(JUnit3TestChecker.java:60)
  at org.apache.maven.surefire.common.junit3.JUnit3TestChecker.accept(JUnit3TestChecker.java:55)
  at org.apache.maven.surefire.common.junit4.JUnit4TestChecker.accept(JUnit4TestChecker.java:53)
  at org.apache.maven.surefire.util.DefaultScanResult.applyFilter(DefaultScanResult.java:102)
  at org.apache.maven.surefire.junit4.JUnit4Provider.scanClassPath(JUnit4Provider.java:309)
  at org.apache.maven.surefire.junit4.JUnit4Provider.setTestsToRun(JUnit4Provider.java:189)
  at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:132)
  at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:379)
  at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:340)
  at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:125)
  at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:413)
Caused by: java.lang.ClassNotFoundException: 
com.github.forinil.psc.exception.NotUpdatableException
  at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
  at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
  ... 18 more

And I have honestly no idea why...

Has someone ever encountered this problem?

Danny Bullis
  • 3,043
  • 2
  • 29
  • 35
Konrad Botor
  • 4,765
  • 1
  • 16
  • 26

3 Answers3

22

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:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.21.0</version>
    <executions>
        <execution>
            <goals>
                <goal>integration-test</goal>
                 <goal>verify</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <classpathDependencyExcludes>
            <classpathDependencyExcludes>${groupId}:${artifactId}</classpathDependencyExcludes>
        </classpathDependencyExcludes>
        <additionalClasspathElements>
            <additionalClasspathElement>${project.build.outputDirectory}</additionalClasspathElement>
        </additionalClasspathElements>
    </configuration>
</plugin>
Konrad Botor
  • 4,765
  • 1
  • 16
  • 26
  • after 3 days of searching you save me :) – Arundev Feb 03 '21 at 22:25
  • Got the following warnings: `[WARNING] The expression ${groupId} is deprecated. Please use ${project.groupId} instead. [WARNING] The expression ${artifactId} is deprecated. Please use ${project.artifactId} instead.` - might need to use those expressions instead. – Danny Bullis Mar 22 '21 at 06:42
  • You also got me unblocked! Thank you! – Danny Bullis Mar 22 '21 at 06:43
  • 4
    This answer didn't work for me on failsafe plugin version 3.0.0-M5, instead needed the following configuration section... ` ${project.build.outputDirectory} ` – KrisG Aug 11 '21 at 11:13
  • The configuration mentioned by @KrisG is the same [configuration used by `spring-boot-starter-parent`](https://github.com/spring-projects/spring-boot/blob/v2.5.6/spring-boot-project/spring-boot-starters/spring-boot-starter-parent/build.gradle#L85-L99). – ordonezalex Nov 10 '21 at 01:15
4

Another option that seems to work is to add a classifier to the spring-boot-maven-plugin configuration. This causes SpringBoot to leave the "default" build target jar alone and instead create the SpringBoot uber jar with the classifier name appended.

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
          <classifier>sb-executable</classifier>
    </configuration>
</plugin>
crig
  • 859
  • 6
  • 19
  • 1
    I consider this solution much cleaner than the ones requiring me to mess with the Failsafe classpath settings – Jim Tough Dec 02 '20 at 15:45
  • should be the accepted answer. fixed a lot of issues with this single line of config. – spi Apr 14 '21 at 08:06
  • This solution works locally but messed up my CI as the proper output jar file now has a suffix. – KrisG Aug 11 '21 at 11:16
  • @KrisG - Correct. If you need to deploy the application, you need to update your CI to use the jar with the suffix as that will be the Springboot runnable/uber jar. – crig Aug 11 '21 at 14:22
2

This worked for me in case of spring-boot project and failsafe plugin version - 3.0.0-M5

<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-failsafe-plugin</artifactid>
<executions>
    <execution>
        <goals>
            <goal>integration-test</goal>
            <goal>verify</goal>
        </goals>
    </execution>
</executions>
<configuration>
    <classesdirectory>${project.build.outputDirectory}</classesdirectory>
</configuration>
</plugin>

Blog referred / Beautiful explanation - https://bjoernkw.com/2020/12/06/using-maven-failsafe-with-spring-boot/

In case if you want to understand what project.build.outputDirectory is - Maven project.build.directory

Thanks !

Akshay Joshi
  • 467
  • 1
  • 9
  • 22