SpringBoot整合JUnit
1.基本要求
要保证测试类所在的目录与上面的包目录对应,这样,在测试的过程中,才能获取到包(com.rql)下注入的Bean和方法。
2.特殊情况
假如测试类不在SpringBoot启动类的包或者子包中,那么需要设定classes:
运行报错,无法进行测试:
java
java.lang.IllegalStateException:
Unable to find a @SpringBootConfiguration,
you need to use @ContextConfiguration or
@SpringBootTest(classes=...) with your test
这个时候,如果还想继续正常测试,可以设定classes = Spring01Application.class
java
@SpringBootTest(classes = Spring01Application.class)
class Spring01ApplicationTests {
@Autowired
private MyDataSource myDataSource;
@Test
void contextLoads() {
System.out.println(myDataSource);
}
}