【问题标题】:How to add log messages in tests (spring boot)?如何在测试中添加日志消息(spring boot)?
【发布时间】:2015-09-12 05:00:35
【问题描述】:

我想在我的项目中添加日志记录(用于控制台),以便在 Spring Boot 中进行测试。 我有我的测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {TestConfig.class})
public class MyTest {

    private final static org.slf4j.Logger LOGGER = LoggerFactory.getLogger(MyTest.class);

    @Autowired
    public UserDao userDao;

    @Test
    public void test1() {
        LOGGER.info("info test");
        LOGGER.debug("debug test");
    }
}

和我的测试配置:

@Configuration
@EnableJpaRepositories("example.dao")
@ComponentScan(basePackageClasses = { MyServiceImpl.class})
@EntityScan({"example.model"})
@Import({DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class TestConfig {

}

我在test/resource 中创建了一个application.properties 文件。 Gradle 将我的资源文件夹视为测试资源。
这是我的application.properties

logging.level.= INFO
logging.level.tests.= INFO
logging.level.org.hibernate= INFO
logging.level.org.springframework= INFO
logging.level.org.apache.cxf= INFO

但是当我运行测试时,我有:

16:59:17.593 [main] INFO  tests.MyTest - info test
16:59:17.594 [main] DEBUG tests.MyTest - debug test

在控制台中。为什么?
我只设置了'INFO'(logging.level.= INFO)。为什么'DEBUG' 在控制台中?怎么能设置成'INFO'

【问题讨论】:

  • logging.level和logging.level.tests末尾的点字符正常吗?

标签: java testing logging spring-boot


【解决方案1】:

这是一个两步过程。

首先,添加spring-boot-starter-test 作为测试依赖项。然后告诉 JUnit 使用刚刚添加的依赖项中的ContextLoader

改变

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {TestConfig.class})
public class MyTest {
    ...

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {TestConfig.class},     
                      loader = SpringApplicationContextLoader.class)
public class MyTest {
    ...

上下文加载器位于第一步添加的spring-boot-starter-test 中,并执行通常由ApplicationBootstrapper 完成的初始化魔法。

根据您使用的 spring-boot 版本,还有其他一些可能性(例如,使用 @SpringApplicationConfiguration 代替 @ContextConfiguration)。你可以在这个春季博客中阅读更多相关信息:https://spring.io/blog/2016/04/15/testing-improvements-in-spring-boot-1-4

【讨论】:

  • SpringApplicationContextLoader 自 v1.4 起已弃用,支持使用 @SpringBootTest 注释测试类,或者在绝对必要时使用 SpringBootContextLoader。在我的例子中,@ContextConfiguration 用于引用beans.xml 文件,@TestPropertySource 用于引用我有日志级别设置的application.properties 文件。新的日志级别仅在测试类开始后生效。在此之前,您仍然会获得 DEBUG 日志记录。
  • @EdwinW 我使用@ContextConfiguration(loader = AnnotationConfigContextLoader.class),默认DEBUG devel 非常嘈杂。 SpringBootContextLoader 只是做正确的工作(尽管在引导期间它仍然会打印一些内部调试消息,直到从 application.yaml 配置日志记录)。感谢分享!
【解决方案2】:

尝试在您的 MyTest 类上添加 @SpringBootConfiguration 而不是 @ContextConfiguration。

按照参考文档中的示例进行操作: http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-testing-spring-boot-applications

Spring Boot 应用程序只是一个 Spring ApplicationContext,因此除了您通常使用普通 Spring 上下文执行的操作之外,无需做任何特别的事情来测试它。不过需要注意的一件事是,Spring Boot 的外部属性、日志记录和其他功能默认情况下只有在使用 SpringApplication 创建时才会安装在上下文中。

【讨论】:

    猜你喜欢
    • 2022-12-23
    • 1970-01-01
    • 2022-09-24
    • 2020-09-25
    • 2021-05-27
    • 2019-01-31
    • 1970-01-01
    • 2015-08-10
    • 1970-01-01
    相关资源
    最近更新 更多