How can to add log in test (spring boot)?

2019-04-29 19:41发布

I want to add logging (for console) in my project, in test part by spring boot. I have my test:

@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");
    }
}

and my test config:

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

}

I create application.properties file in test/resource. And Gradle see my resource folder as resource for tests. My 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

But when I run my test, I have:

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

in console, why? I set just 'INFO'(logging.level.= INFO), Why 'DEBUG' in console? How can to set just 'INFO'?

2条回答
疯言疯语
2楼-- · 2019-04-29 19:50

It's a two step proccess.

First, add spring-boot-starter-test as a test dependency. Then tell JUnit to use the ContextLoader from the just added dependency.

Change

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

to

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

The context loader lives in spring-boot-starter-test added in the first step and does the initialization magic normally done by the ApplicationBootstrapper.

Depending on the spring-boot version you are using there are some other possibilities (e.g. using @SpringApplicationConfiguration in place of @ContextConfiguration). You can read more about that in this spring blog: https://spring.io/blog/2016/04/15/testing-improvements-in-spring-boot-1-4

查看更多
SAY GOODBYE
3楼-- · 2019-04-29 19:57

Try adding @SpringBootConfiguration instead of @ContextConfiguration on your MyTest class.

Follow the example in the Reference doc: http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-testing-spring-boot-applications

A Spring Boot application is just a Spring ApplicationContext so nothing very special has to be done to test it beyond what you would normally do with a vanilla Spring context. One thing to watch out for though is that the external properties, logging and other features of Spring Boot are only installed in the context by default if you use SpringApplication to create it.

查看更多
登录 后发表回答