当前位置:   article > 正文

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

mockmvc post

一、引入MockMvc依赖

使用MockMvc,必须要引入依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

二、具体演示

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();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

controller类代码

	@GetMapping("/trigger/{itemCode}")
    public R<Void> execSchedule(@PathVariable("itemCode") String itemCode) {
        return patrolResultService.execSchedule(itemCode);
    }
  • 1
  • 2
  • 3
  • 4

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();
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

controller类代码

	@GetMapping("/list")
    public TableDataInfo<PatrolResult> getRunStatsDataAggs(PatrolQueryConfig patrolQueryConfig) {
        List<PatrolResult> patrolResults = patrolResultService.getPatrolHisResultAggs(patrolQueryConfig);
        return TableDataInfo.build(patrolResults);
    }
  • 1
  • 2
  • 3
  • 4
  • 5

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();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

controller类代码

	@PostMapping("/list")
    public TableDataInfo<PatrolResult> getRunStatsDataAggs(@RequestBody PatrolQueryConfig patrolQueryConfig) {
        List<PatrolResult> patrolResults = patrolResultService.getPatrolHisResultAggs(patrolQueryConfig);
        return TableDataInfo.build(patrolResults);
    }
  • 1
  • 2
  • 3
  • 4
  • 5

三、总结

  • 使用MockMvc可以做到controller层的测试。
  • 在初始化mockMvc的时候:
    (1)如果想测试单个controller
    mockMvc = MockMvcBuilders.standaloneSetup(mockMvcController).build();
    (2)如果想测试所有controller
    mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号