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'
?
It's a two step proccess.
First, add
spring-boot-starter-test
as a test dependency. Then tell JUnit to use theContextLoader
from the just added dependency.Change
to
The context loader lives in
spring-boot-starter-test
added in the first step and does the initialization magic normally done by theApplicationBootstrapper
.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-4Try 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