Springboot项目:使用MockMvc测试get和post接口(含单个和多个请求参数场景)

Springboot项目:使用MockMvc测试get和post接口(含单个和多个请求参数场景)

码农世界 2024-06-04 后端 95 次浏览 0个评论

一、引入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 R 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 getRunStatsDataAggs(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 TableDataInfo getRunStatsDataAggs(@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();

转载请注明来自码农世界,本文标题:《Springboot项目:使用MockMvc测试get和post接口(含单个和多个请求参数场景)》

百度分享代码,如果开启HTTPS请参考李洋个人博客
每一天,每一秒,你所做的决定都会改变你的人生!

发表评论

快捷回复:

评论列表 (暂无评论,95人围观)参与讨论

还没有评论,来说两句吧...

Top