maven 引入
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
idea alt+enter 生成测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestServiceTest {
@Autowired
private TestService testService;
@Test
public void test1() {
//此处测试service层面,controller/dao 层类似测试
testService.test();
// Thread.sleep(60000);
}
}
如果测试的是多线程方法,例如带@Async 注解,需要在测试方法中加入线程修改时间,防止线程提前打断,子线程没有执行完就结束了,例如下图所示:如果不加入等待时间,提前杀掉了线程池,导致线程执行错误
2021-08-26 01:06:43.161 |-ERROR [async-service-1] SimpleAsyncUncaughtExceptionHandler [39] -| Unexpected exception occurred invoking async method: public void com.service.TestService.issueGroupByJsonTest(java.lang.String,boolean) throws java.lang.InterruptedException