众所周知咱们玩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());
}
}