若是是使用spring-boot 1.4如下的版本java
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = 启动类.class) public class ApplicationTest { //代码省略 }
使用@SpringApplicationConfiguration注解spring
若是spring-boot是1.4以上的版本spring-boot
@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = 启动类.class) public class ApplicationTest { //代码省略 }
使用@SpringBootTest注解测试
PS:本人测试的时候,由于是1.4以上的版本,使用@SpringApplicationConfiguration注解一直报错,跑注解源码一看,发现下面一段话:.net
@deprecated as of 1.4 in favor of {@link SpringBootTest} or direct use of {@link SpringBootContextLoader}code
弃用:1.4版本支持 @SpringBootTest注解,或者直接使用 @SpringBootContextLoader注解,get
改过来,使用@SpringBootTest就能正常跑了!源码