0


Springboot项目中Controller层的单元测试

源码展示:
原来的controller类:

importlombok.extern.slf4j.Slf4j;importorg.springframework.stereotype.Service;importorg.springframework.web.bind.annotation.*;@RestController@RequestMapping("/web")@Slf4j@ServicepublicclassWebController{@RequestMapping("/get")publicStringget(){
        log.info("get");return"get";}@PostMapping("/post")publicStringpost(@RequestBodyStudent student){
        log.info("post method student={}",student );return"post success";}@PostMapping("/postHead")publicStringpostHead(@RequestHeader("token")String token){
        log.info("token : "+token);return"post head token:"+token;}}

自己定义的一个测试对象student:

importlombok.Data;@DatapublicclassStudent{privateString name;privateint age;}

测试代码:

packageorg.qjg.controller;importcn.hutool.core.lang.Assert;importcom.alibaba.fastjson.JSON;importlombok.SneakyThrows;importlombok.extern.slf4j.Slf4j;importorg.junit.jupiter.api.BeforeEach;importorg.junit.jupiter.api.Test;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.http.HttpHeaders;importorg.springframework.http.MediaType;importorg.springframework.test.web.servlet.MockMvc;importorg.springframework.test.web.servlet.MvcResult;importorg.springframework.test.web.servlet.request.MockMvcRequestBuilders;importorg.springframework.test.web.servlet.result.MockMvcResultMatchers;importorg.springframework.test.web.servlet.setup.MockMvcBuilders;importorg.springframework.util.LinkedMultiValueMap;importorg.springframework.util.MultiValueMap;importorg.springframework.web.context.WebApplicationContext;importjavax.annotation.Resource;importjava.nio.charset.StandardCharsets;@Slf4j@SpringBootTestclassWebControllerTest{privateMockMvc mockMvc;privateHttpHeaders headers;@ResourceprivateWebApplicationContext webApplicationContext;privateString baseUrl ="/web";@BeforeEachpublicvoidinit(){
        mockMvc =MockMvcBuilders.webAppContextSetup(webApplicationContext).build();MultiValueMap<String,String> map =newLinkedMultiValueMap<>();//或者使用普通的map也是可以的
        map.add("token","qjg-token");

        headers =newHttpHeaders();
        headers.putAll(map);;}@SneakyThrows@Testvoidget(){MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get(baseUrl +"/get").headers(headers).contentType(MediaType.APPLICATION_JSON)).andExpect(MockMvcResultMatchers.status().isOk()).andReturn();String contentAsString = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
        log.info("contentAsString: "+ contentAsString);}@SneakyThrows@Testvoidpost(){Student student =newStudent();
        student.setAge(18);
        student.setName("张三");MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post(baseUrl +"/post").contentType(MediaType.APPLICATION_JSON).content(JSON.toJSONString(student)).headers(headers)).andExpect(MockMvcResultMatchers.status().isOk()).andReturn();String contentAsString = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
        log.info("contentAsString: "+ contentAsString);}@SneakyThrows@TestvoidpostHead(){MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post(baseUrl +"/postHead").contentType(MediaType.APPLICATION_JSON).content("").headers(headers)).andExpect(MockMvcResultMatchers.status().isOk()).andReturn();String contentAsString = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);Assert.isTrue("post head token:qjg-token".equals(contentAsString));}}

本文转载自: https://blog.csdn.net/u013583931/article/details/137901604
版权归原作者 奔跑_小子 所有, 如有侵权,请联系我们删除。

“Springboot项目中Controller层的单元测试”的评论:

还没有评论