• Springboot项目中Controller层的单元测试


    源码展示
    原来的controller类

    import lombok.extern.slf4j.Slf4j;
    import org.springframework.stereotype.Service;
    import org.springframework.web.bind.annotation.*;
    
    @RestController
    @RequestMapping("/web")
    @Slf4j
    @Service
    public class WebController {
    
    
        @RequestMapping("/get")
        public String get(){
            log.info("get");
            return "get";
        }
    
        @PostMapping("/post")
        public String post(@RequestBody Student student){
            log.info("post method student={}",student );
            return "post success";
        }
    
    
        @PostMapping("/postHead")
        public String postHead(@RequestHeader("token") String token){
            log.info("token : "+token);
            return "post head token:"+token;
        }
    
    
    }
    
    
    • 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
    • 31
    • 32
    • 33

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

    
    
    import lombok.Data;
    
    @Data
    public class Student {
        private String name;
        private int age;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    测试代码:

    package org.qjg.controller;
    
    import cn.hutool.core.lang.Assert;
    import com.alibaba.fastjson.JSON;
    import lombok.SneakyThrows;
    import lombok.extern.slf4j.Slf4j;
    import org.junit.jupiter.api.BeforeEach;
    import org.junit.jupiter.api.Test;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.MediaType;
    import org.springframework.test.web.servlet.MockMvc;
    import org.springframework.test.web.servlet.MvcResult;
    import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
    import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
    import org.springframework.test.web.servlet.setup.MockMvcBuilders;
    import org.springframework.util.LinkedMultiValueMap;
    import org.springframework.util.MultiValueMap;
    import org.springframework.web.context.WebApplicationContext;
    
    import javax.annotation.Resource;
    import java.nio.charset.StandardCharsets;
    
    
    @Slf4j
    @SpringBootTest
    class WebControllerTest {
    
        private MockMvc mockMvc;
    
        private HttpHeaders headers;
    
        @Resource
        private WebApplicationContext webApplicationContext;
    
        private String baseUrl = "/web";
    
    
        @BeforeEach
        public void init() {
            mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
            MultiValueMap<String, String> map = new LinkedMultiValueMap<>();//或者使用普通的map也是可以的
            map.add("token", "qjg-token");
    
            headers = new HttpHeaders();
            headers.putAll(map);
            ;
        }
    
        @SneakyThrows
        @Test
        void get() {
            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
        @Test
        void post() {
    
            Student student = new Student();
            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
        @Test
        void postHead() {
            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));
        }
    }
    
    • 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
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
  • 相关阅读:
    基于 Jenkins + Kubernetes(混合集群) + Argo CD 的完整 DevOps 流程记录(1) - 环境部署
    详解Volatile关键字
    三年了,你知道我是什么怎么过来的吗?
    Oracle 慢查询排查步骤
    设计模式【1】-- 单例模式
    上海亚商投顾: 市场震荡整理 飞机、消费股表现强势
    模块化开发_php中使用redis
    Ubuntu打造家用NAS三——网盘与影视中心
    欧拉定理公式(包括欧拉降幂)
    Spring MVC
  • 原文地址:https://blog.csdn.net/u013583931/article/details/137901604