Jay
Jay

Reputation: 71

Error while using JUnit5 with spring boot 2

I am getting following error while running JUnit5 with SpringBoot. Here is the stack trace that I get when I run ./gradlew clean test

    

:junitPlatformTest
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/logging/log4j/util/ReflectionUtil
        at org.apache.logging.log4j.jul.AbstractLoggerAdapter.getContext(AbstractLoggerAdapter.java:34)
        at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:46)
        at org.apache.logging.log4j.jul.LogManager.getLogger(LogManager.java:89)
        at java.util.logging.LogManager.demandLogger(LogManager.java:551)
        at java.util.logging.Logger.demandLogger(Logger.java:455)
        at java.util.logging.Logger.getLogger(Logger.java:502)
        at org.junit.platform.commons.logging.LoggerFactory$DelegatingLogger.<init>(LoggerFactory.java:62)
        at org.junit.platform.commons.logging.LoggerFactory.getLogger(LoggerFactory.java:49)
        at org.junit.platform.launcher.core.DefaultLauncher.<clinit>(DefaultLauncher.java:44)
        at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:59)
        at org.junit.platform.console.tasks.ConsoleTestExecutor.executeTests(ConsoleTestExecutor.java:61)
        at org.junit.platform.console.tasks.ConsoleTestExecutor.lambda$execute$0(ConsoleTestExecutor.java:57)
        at org.junit.platform.console.tasks.CustomContextClassLoaderExecutor.invoke(CustomContextClassLoaderExecutor.java:33)
        at org.junit.platform.console.tasks.ConsoleTestExecutor.execute(ConsoleTestExecutor.java:57)
        at org.junit.platform.console.ConsoleLauncher.executeTests(ConsoleLauncher.java:85)
        at org.junit.platform.console.ConsoleLauncher.execute(ConsoleLauncher.java:75)
        at org.junit.platform.console.ConsoleLauncher.execute(ConsoleLauncher.java:48)
        at org.junit.platform.console.ConsoleLauncher.main(ConsoleLauncher.java:40)
Caused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.util.ReflectionUtil
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 18 more

The github url for above code is github link

Upvotes: 0

Views: 841

Answers (1)

Marc Philipp
Marc Philipp

Reputation: 1908

This is caused by mixing different versions of Log4J on the test runtime classpath. Spring Boot pulls in 2.10.0, your build.gradle declares 2.6.2.

If you remove the version from your dependency definition, Spring Boot will define the version and it will work.

So, please replace these two lines

testRuntime("org.apache.logging.log4j:log4j-core:${log4jVersion}")
testRuntime("org.apache.logging.log4j:log4j-jul:${log4jVersion}")

with these:

testRuntime("org.apache.logging.log4j:log4j-core")
testRuntime("org.apache.logging.log4j:log4j-jul")

Alternatively, you can delete the logManager property from the junitPlatform extension and remove both dependencies.

Upvotes: 2

Related Questions