概述
本文主要介绍如何在spring boot项目中使用junit测试模块功能
方法一/使用spring-boot自带的junit测试功能
引入maven依赖
<!-- Test start-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<!-- Test end -->
测试类,需要和启动类保持同级
@RunWith(SpringRunner.class)
@SpringBootTest
@EnableTransactionManagement
public class ApplicationTests {
@Resource
private TrtcMixStreamService trtcMixStreamService;
@Test
public void test() throws InterruptedException {
TrtcStartMixStreamModel trtcStartMixStreamModel = new TrtcStartMixStreamModel();
trtcStartMixStreamModel.setRoomId(84L);
trtcStartMixStreamModel.setUserId("180");
trtcStartMixStreamModel.setMixStreamId("szpc_84_playUrl");
trtcStartMixStreamModel.initBaseModelInfo("1255516392", "cee491af9d286ff3d3ca3ec6a1d2d621", "1400321747");
trtcMixStreamService.openCloudMixStream(trtcStartMixStreamModel);
System.out.println("等待10秒");
Thread.sleep(10000);
trtcMixStreamService.cancelCloudMixStream(trtcStartMixStreamModel);
}
// @Test
public void test2() throws InterruptedException {
TrtcStartMixStreamModel trtcStartMixStreamModel = new TrtcStartMixStreamModel();
trtcStartMixStreamModel.setRoomId(86L);
trtcStartMixStreamModel.setUserId("180");
trtcStartMixStreamModel.setMixStreamId("szpc_86_playUrl");
trtcStartMixStreamModel.initBaseModelInfo("1255516392", "cee491af9d286ff3d3ca3ec6a1d2d621", "1400321747");
trtcMixStreamService.cancelCloudMixStream(trtcStartMixStreamModel);
}
}
总结
spring boot junit 已经集成了junit和mokito,使用起来比较方便,完善的代码需要jnit测试保证