SpringBoot单元测试中SLF4J依赖冲突排查求助
SpringBoot单元测试中SLF4J依赖冲突排查求助
大家好,我最近在做一个SpringBoot+Maven的项目,写了个简单的单元测试来验证应用能正常启动不抛异常,结果遇到了SLF4J的依赖冲突问题,折腾了半天没搞定,来求助大家帮忙分析下~
问题背景
我写了一个基础的SpringBoot启动类,用了Lombok的@Slf4j注解,还配置了AWS SDK的日志参数。然后写了个单元测试直接调用main方法,结果运行测试时触发了SLF4J的依赖冲突错误,导致测试失败。
相关代码
启动类 ReporterApplication
package com.dummy.reporter; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; /** * Application starting point. */ @Slf4j @EnableAsync @EnableScheduling @SpringBootApplication public class ReporterApplication { static { System.setProperty("aws.crt.log.level", "Warn"); System.setProperty("aws.crt.log.destination", "Stdout"); log.debug("Configured AWS SDK log"); } public static void main(String[] args) { SpringApplication.run(ReporterApplication.class, args); } }
单元测试类 ReporterApplicationTest
@Test public void testMain() { // This test is just a placeholder to ensure that the main method can be invoked without exceptions. // In a real-world scenario, you might want to mock the Spring context or check for specific beans. ReporterApplication.main(new String[]{}); // If the application starts without throwing an exception, we consider it successful. assertTrue(true); }
测试错误日志
运行测试后得到的错误输出:
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.073 s <<< FAILURE! -- in com.dummy.reporter.ReporterApplicationTest [ERROR] com.dummy.reporter.ReporterApplicationTest.testMain -- Time elapsed: 0.054 s <<< ERROR! java.lang.IllegalStateException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.slf4j.helpers.NOPLoggerFactory loaded from file:/Users/e1083827/.m2/repository/org/slf4j/slf4j-api/1.7.36/slf4j-api-1.7.36.jar). If you are using WebLogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml at org.springframework.util.Assert.state(Assert.java:101) at org.springframework.boot.logging.logback.LogbackLoggingSystem.getLoggerContext(LogbackLoggingSystem.java:410) at org.springframework.boot.logging.logback.LogbackLoggingSystem.beforeInitialize(LogbackLoggingSystem.java:129) at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationStartingEvent(LoggingApplicationListener.java:238) at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:220) at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:180) at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:173) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:151) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:133) at org.springframework.boot.context.event.EventPublishingRunListener.multicastInitialEvent(EventPublishingRunListener.java:136) at org.springframework.boot.context.event.EventPublishingRunListener.starting(EventPublishingRunListener.java:75) at org.springframework.boot.SpringApplicationRunListeners.lambda$starting$0(SpringApplicationRunListeners.java:54) at java.base/java.lang.Iterable.forEach(Iterable.java:75) at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:118) at org.springframework.boot.SpringApplicationRunListeners.starting(SpringApplicationRunListeners.java:54) at org.springframework.boot.SpringApplication.run(SpringApplication.java:310) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1361) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1350) at com.dummy.reporter.ReporterApplication.main(ReporterApplication.java:25) at com.dummy.reporter.ReporterApplicationTest.testMain(ReporterApplicationTest.java:13) at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) at java.base/java.lang.reflect.Method.invoke(Method.java:580) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
我的疑问
从错误信息看,是SLF4J的实现冲突了:一边有Logback在类路径,另一边又加载了org.slf4j.helpers.NOPLoggerFactory。我想请教大家:
- 具体是哪两个依赖库导致了这个冲突?
- 我应该移除哪个依赖来解决这个问题?
- 有没有更高效的Maven命令或工具,能帮我快速定位依赖树里的冲突项?
内容来源于stack exchange
相关产品推荐
相关产品推荐

