Spring Boot 1.5.2 Junit测试

来源:互联网 发布:vb创意小程序 编辑:程序博客网 时间:2024/05/18 14:29
   最近由于项目原因,重拾junit测试,,由于springboot在1.4版本以后,junit使用的注解不一样了,导致走了不少弯路,现在将过程记录下来:
1. 首先maven中添加junit4依赖:


junit
junit
4.12
test



2.在IDE中新增JunitTest类
@RunWith(SpringRunner.class) //14.版本之前用的是SpringJUnit4ClassRunner.class
@SpringBootTest(classes = Application.class) //1.4版本之前用的是//@SpringApplicationConfiguration(classes = Application.class)
public class SystemInfoServiceImplTest {

@Autowired
private ISystemInfoService systemInfoservice;

@Test
public void add() throws Exception {

}

@Test
public void findAll() throws Exception {

}

}
主要是注解的更改,如果注解用的不对,会报各种奇怪的问题,例如applicationContext找不到,datasource实例化失败等等。
3. 为了支持上面两个注解,maven文件中要用对依赖以及版本,我当时添加SpringRunner.class所在的依赖jar时,由于用了idea的auto-imported,IDE自动导入了版本是3.x的,实际应该导入4.x,我一直以为idea导入的是正确的,导致在这上面费时颇多,后来我手工写入就解决了。下面是正确的spring boot test的maven依赖


org.springframework
spring-test
4.3.7.RELEASE


org.springframework.boot
spring-boot-test
test