将JUnit5与spring boot 2一起使用时出错

时间:2018-01-19 17:44:00

标签: junit5

使用SpringBoot运行JUnit5时出现以下错误。 这是我运行时获得的堆栈跟踪 ./gradlew清洁测试



    

: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
&#13;
&#13;
&#13;

上面代码的github网址是github link

1 个答案:

答案 0 :(得分:2)

这是由于在测试运行时类路径中混合使用不同版本的Log4J引起的。 Spring Boot拉入2.10.0,你的build.gradle声明为2.6.2。

如果从依赖关系定义中删除版本,Spring Boot将定义版本并且它将起作用。

所以,请替换这两行

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

用这些:

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

或者,您可以从logManager扩展名中删除junitPlatform属性并删除这两个依赖项。