众所周知咱们玩SpringBoot的时候自己写在Impl方法里边Autowired或者Resource了很多依赖,如果像跟往常一样写个main方法来测是行不通的,那么这种该怎么测呢?请参考下面的代码:
package com.test;
import xxx;
//重点是引入如下2个注解就好,不要问为什么
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class Test {
//比如说你在TestServiceImpl里面写了个test()方法
//里边依赖了很多Autowired写main方法是不行的
@Autowired
private TestService testService;
//如果你是IDEA就点小三角执行 或者 右键Run 'Test'也行
@Test
public void deviceCurrentData() throws Exception{
TestVo vo = testService.test("test869298057215725");
System.out.println(vo.toString());
}
}
本文介绍了一种在SpringBoot项目中进行单元测试的方法。通过使用@RunWith(SpringRunner.class)和@SpringBootTest注解,可以在不启动整个应用的情况下测试那些依赖于@Autowired注入的服务方法。
2万+

被折叠的 条评论
为什么被折叠?



