赞
踩
使用MockMvc,必须要引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
测试类代码
@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);
}
测试类代码:
@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);
}
测试类代码
@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);
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。