一、引入MockMvc依赖
使用MockMvc,必须要引入依赖
org.springframework.boot spring-boot-starter-test test
二、具体演示
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 RexecSchedule(@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 TableDataInfogetRunStatsDataAggs(PatrolQueryConfig patrolQueryConfig) { List 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 TableDataInfogetRunStatsDataAggs(@RequestBody PatrolQueryConfig patrolQueryConfig) { List 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();
还没有评论,来说两句吧...