一开始做项目的时候没有写过单元测试什么的,都是自给自足,就说前后一套流程自己完成。现在只需要负责编写接口就是。现在呢,有postman、脚本测试等,用postman测试还被叼了一顿说没有覆盖率,说要什么单元测试,说的一脸蒙蔽,因为没有做过单元测试的我。一开始做单元测试我就直接调用service层进行测,这时又被叼,说你这测个毛线啊,还不如不测。后面才了解Mock测试,好了话不多说,上案例。
一、首先添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
二、创建测试包与开发包同级一样包名,如图所示:
三、编写测试类
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* @author ZHY
* @since 2021/6/1 19:43
*/
@SpringBootTest(classes = Zkt5gBuAdminApplication.class)
@AutoConfigureMockMvc
@WebAppConfiguration
@SpringJUnitConfig
public class ActivityTest {
@Autowired
private MockMvc mockMvc;
@Test
public void fun() throws Exception {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Authorization","eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpZCI6IjE4NzIwOTM1MjA1IiwiZXhwIjoxNjIyNTQ2OTc1fQ.3LD_9FWl2QgNFpRLQgxrcciLA1CFPdhp02_ARShgwsGgDmPHGI0BYkKwr7p1gGzPS2gK8ECzgalV-bL0xl_x4A");
mockMvc.perform(MockMvcRequestBuilders.get("/bu/createActivity/findActivityInfoPage")
.param("pageIndex","1","pageSize","10")
.headers(httpHeaders))
.andExpect(status().isOk())
.andDo(MockMvcResultHandlers.print());
}
@Test
public void fun4() throws Exception {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Authorization","eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpZCI6IjE4NzIwOTM1MjA1IiwiZXhwIjoxNjIyNTQ2OTc1fQ.3LD_9FWl2QgNFpRLQgxrcciLA1CFPdhp02_ARShgwsGgDmPHGI0BYkKwr7p1gGzPS2gK8ECzgalV-bL0xl_x4A");
mockMvc.perform(MockMvcRequestBuilders.post("/bu/createActivity/deleteActivity")
.param("id","126")
.headers(httpHeaders))
.andExpect(status().isOk())
.andDo(MockMvcResultHandlers.print());
}
@Test
public void fun5() throws Exception {
String json ="{\"id\":126,\"type\":1,\"cpnType\":1,\"cpnName\":\"折扣优惠券\",\"grantTotal\":1000,\"useThreshold\":1,\"fullAmount\":220,\"preferContent\":99.00,\"cpnStartTime\":\"2021-06-01\",\"cpnEndTime\":\"2021-06-10\",\"instructions\":\"123131311,123132132131,132131311\",\"activityName\":\"折扣活动\",\"createTime\":\"2021-05-25 10:55:22\",\"endTime\":\"2021-06-01 10:55:22\",\"activityRange\":1,\"channel\":1,\"prologue\":\"语音开场白\",\"introduce\":\"详情介绍\",\"closing\":\"感谢结束语\",\"noticeSms\":\"营销短信通知内容\",\"activityLinkurl\":\"本次活动的优惠券链接\",\"smsSign\":\"招客通\",\"noticeDay\":\"23\",\"callTime\":\"19:00\",\"groupId\":\"1023,1024,1025\",\"referralNum\":\"2000\"}";
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Authorization","eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpZCI6IjE4NzIwOTM1MjA1IiwiZXhwIjoxNjIyNTQ2OTc1fQ.3LD_9FWl2QgNFpRLQgxrcciLA1CFPdhp02_ARShgwsGgDmPHGI0BYkKwr7p1gGzPS2gK8ECzgalV-bL0xl_x4A");
mockMvc.perform(MockMvcRequestBuilders.post("/bu/createActivity/updateActivity")
.content(json)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.headers(httpHeaders))
.andExpect(status().isOk())
.andDo(MockMvcResultHandlers.print());
}
}
四、测试结果,如图所示:
上面是执行SQL查询结果,下面是响应给前端的结果