SpringBoot使用MockMvc测试get和post接口的示例代码
更新时间:2024年06月24日 11:51:50 作者:飞翔的佩奇
Spring Boot MockMvc是一个用于单元测试的模块,它是Spring框架的一部分,专注于简化Web应用程序的测试,MockMvc主要用来模拟一个完整的HTTP请求-响应生命周期,本文给大家介绍了SpringBoot使用MockMvc测试get和post接口,需要的朋友可以参考下
一、引入MockMvc依赖
使用MockMvc,必须要引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
二、具体演示
1、GET请求(单个参数)
测试类代码
@ExtendWith(MockitoExtension.class) class AppControllerTest { private MockMvc mockMvc; @Mock private IDB2PatrolResultService idb2PatrolResultService; @InjectMocks private DB2PatrolResultController db2PatrolResultController; @BeforeEach void setUp() { this.mockMvc = MockMvcBuilders.standaloneSetup(db2PatrolResultController).build(); } @Test void testGet() throws Exception { this.mockMvc.perform(MockMvcRequestBuilders.get("/dbm/patrolResult/trigger/{itemCode}","121")) .andExpect(status().isOk()) .andDo(MockMvcResultHandlers.print()) .andReturn(); } }
controller类代码
@GetMapping("/trigger/{itemCode}") public R<Void> execSchedule(@PathVariable("itemCode") String itemCode) { return patrolResultService.execSchedule(itemCode); }
2、GET请求(多个参数)
测试类代码:
@ExtendWith(MockitoExtension.class) class AppControllerTest { private MockMvc mockMvc; @Mock private IDB2PatrolResultService idb2PatrolResultService; @InjectMocks private DB2PatrolResultController db2PatrolResultController; @BeforeEach void setUp() { this.mockMvc = MockMvcBuilders.standaloneSetup(db2PatrolResultController).build(); } @Test void testPatrolResult() throws Exception { PatrolQueryConfig patrolQueryConfig = new PatrolQueryConfig(); patrolQueryConfig.setItemLevel("0"); patrolQueryConfig.setStatusType("0"); String jsonStr = JSONUtil.toJsonStr(patrolQueryConfig); this.mockMvc.perform(MockMvcRequestBuilders.get("/dbm/patrolResult/list") .param("itemLevel","1") .param("statusType","2")) .andExpect(status().isOk()) .andDo(MockMvcResultHandlers.print()) .andReturn(); } }
controller类代码
@GetMapping("/list") public TableDataInfo<PatrolResult> getRunStatsDataAggs(PatrolQueryConfig patrolQueryConfig) { List<PatrolResult> patrolResults = patrolResultService.getPatrolHisResultAggs(patrolQueryConfig); return TableDataInfo.build(patrolResults); }
3、POST请求(多个参数)
测试类代码
@ExtendWith(MockitoExtension.class) class AppControllerTest { private MockMvc mockMvc; @Mock private IDB2PatrolResultService idb2PatrolResultService; @InjectMocks private DB2PatrolResultController db2PatrolResultController; @BeforeEach void setUp() { this.mockMvc = MockMvcBuilders.standaloneSetup(db2PatrolResultController).build(); } @Test void testPost() throws Exception { PatrolQueryConfig patrolQueryConfig = new PatrolQueryConfig(); patrolQueryConfig.setItemLevel("0"); patrolQueryConfig.setStatusType("0"); String jsonStr = JSONUtil.toJsonStr(patrolQueryConfig); this.mockMvc.perform(MockMvcRequestBuilders.post("/dbm/patrolResult/list") .contentType(MediaType.APPLICATION_JSON_VALUE) .content(jsonStr) .header(HttpHeaders.ACCEPT,"application/json")) .andExpect(status().isOk()) .andDo(MockMvcResultHandlers.print()) .andReturn(); } }
controller类代码
@PostMapping("/list") public TableDataInfo<PatrolResult> getRunStatsDataAggs(@RequestBody PatrolQueryConfig patrolQueryConfig) { List<PatrolResult> patrolResults = patrolResultService.getPatrolHisResultAggs(patrolQueryConfig); return TableDataInfo.build(patrolResults); }
三、总结
- 使用MockMvc可以做到controller层的测试。
- 在初始化mockMvc的时候:
(1)如果想测试单个controller
mockMvc = MockMvcBuilders.standaloneSetup(mockMvcController).build();
(2)如果想测试所有controller
mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
以上就是SpringBoot使用MockMvc测试get和post接口的示例代码的详细内容,更多关于SpringBoot MockMvc测试接口的资料请关注脚本之家其它相关文章!
最新评论