登录单元测试失败

时间:2019-04-08 16:23:31

标签: unit-testing spring-boot jenkins logback

我正试图解决单元测试中Jenkins构建中抛出的以下异常,该异常覆盖了Spring Boot的logback配置。 请注意,IntelliJ中不会发生此异常

Spring是否仍可能尝试使用默认的logback.xml?我尝试从classpath:删除logging.config前缀,并使用DirtiesContext无济于事。

Logging system failed to initialize using configuration from 'classpath:some/package/logback-test-sample.xml'
java.lang.IllegalStateException: Logback configuration error detected: 
ERROR in ch.qos.logback.core.joran.spi.Interpreter@3:117 - no applicable action for [springProperty], current ElementPath  is [[configuration][springProperty]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@4:113 - no applicable action for [springProperty], current ElementPath  is [[configuration][springProperty]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@5:116 - no applicable action for [springProperty], current ElementPath  is [[configuration][springProperty]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@6:113 - no applicable action for [springProperty], current ElementPath  is [[configuration][springProperty]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@7:113 - no applicable action for [springProperty], current ElementPath  is [[configuration][springProperty]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@8:97 - no applicable action for [springProperty], current ElementPath  is [[configuration][springProperty]]
    at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:169)

这是导致异常的测试

@SpringBootApplication
protected static class MyTest {
     public static void main(String... args) {
          SpringApplication.run(MyTest.class, args);
     }
}

@Test
public void myTest() {
     ClassPathResource properties = new ClassPathResource("some/package/logging.properties");
     Properties props = new Properties();
     props.load(properties.getInputStream());
     try (ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder().web(WebApplicationType.NONE).properties(props).profiles("some-profile").sources(MyTest.class).run()) {
            assertThat(applicationContext.getEnvironment().getProperty("logging.config")).isNotBlank().isEqualTo("classpath:some/package/logback-test-sample.xml");
     }
}

file.properties

logging.config=classpath:some/package/logback-test-sample.xml

0 个答案:

没有答案