0

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Removing those two lines in the current project fails the project to recognize any test. The build is successful with 0 test. If I comment out vintage version, all tests run. However, at this time I am using 4.12 and not 5 testRuntime("org.junit.vintage:junit-vintage-engine:${junitVintageVersion}")
I've clarified my instructions. Hope that helps!
After removing ${log4jVersion} as you mentioned, I am able to run the tests. However @Disabled annotation in the [github.com/jsaraiya1/springboot-junit5/blob/master/src/test/… (second test) does not seem to be recognized. The gradle build still tries to run the test that is marked as @Disabled and fails it. I see the same behavior when I completely remove logManager property from junitPlaform extension as well.
That's because the test is currently executed as a JUnit 4 test by the junit-vintage-engine. You need to switch from org.junit.Test to org.junit.jupiter.api.Test and remove the @RunWith annotation.
Thanks. Yes. I had wrong import in this test. So, Simply commenting out junit4, junitvintage version dependancies along with all log4j-core and log4j-jul dependancies made all JUnit5 tests work fine. However, I need to be able to run Junit4 and 5 both in the same project. I modified the [github] (github.com/jsaraiya1/springboot-junit5) to add one more JUnit4 only test class. The tests in this class do not get recognized any more. What is the best configuration to have Junit4 and Junit5 both in the same project.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.