Spring MVC单元测试笔记整理

使用@WebMvcTest注解
  1. 编写单元测试类

    java 复制代码
    @WebMvcTest
    class HelloControllerWebMvcTest {
        private MockMvc mockMvc;
        @MockBean
        private HelloService helloService ;
        //
        @BeforeEach
        void setup(WebApplicationContext wac) {
            this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
        }
        @Test
        void index() throws Exception {
            given(this.helloService.hello("张三")).willReturn("hello, 张三") ;
            //
            MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/hello/index")
                    .accept(MediaType.APPLICATION_JSON)
                    .contentType(MediaType.APPLICATION_JSON);
            mockMvc.perform(requestBuilder)
                    .andExpect(status().isOk())
                    .andExpect(content().contentType("application/json"))
                    .andDo(print()) ;
        }
    }
使用@SpringJUnitWebConfig注解
  1. 编写配置类

    java 复制代码
    @Configuration
    @EnableWebMvc
    @ComponentScan(basePackages = "com.yicj.study.controller")
    public class WebMvcConfig {
        @Bean
        public HelloService helloService(){
            return new HelloServiceImpl() ;
        }
    }
  2. 编写单元测试

    java 复制代码
    @SpringJUnitWebConfig(classes = WebMvcConfig.class)
    class HelloControllerWebConfigTest {
        MockMvc mockMvc;
        @BeforeEach
        void setup(WebApplicationContext wac) {
            this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
        }
        @Test
        void index() throws Exception {
            MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("http://localhost:8081/hello/index")
                    .accept(MediaType.APPLICATION_JSON)
                    .contentType(MediaType.APPLICATION_JSON);
            mockMvc.perform(requestBuilder)
                    .andExpect(status().isOk())
                    .andExpect(content().contentType("application/json"))
                    .andDo(print()) ;
        }
    }
使用standaloneSetup模式
  1. 编写单元测试

    java 复制代码
    class HelloControllerStandaloneTest {
        MockMvc mockMvc;
        // 这里不能使用MockBean
        HelloService helloService ;
        @BeforeEach
        void setup() {
            helloService = new HelloServiceImpl();
            this.mockMvc = MockMvcBuilders.standaloneSetup(new HelloController(helloService))
                    //.defaultRequest(MockMvcRequestBuilders.get("/hello/index").accept(MediaType.APPLICATION_JSON))
                    .alwaysExpect(status().isOk())
                    .alwaysExpect(content().contentType("application/json"))
                    .build();
        }
        @Test
        void index() throws Exception {
            //given(this.helloService.hello("张三")).willReturn("hello, 张三") ;
            MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/hello/index")
                    .accept(MediaType.APPLICATION_JSON)
                    .contentType(MediaType.APPLICATION_JSON);
            mockMvc.perform(requestBuilder)
                    .andExpect(status().isOk())
                    .andExpect(content().contentType("application/json"))
                    .andDo(print()) ;
        }
    }
相关推荐
二宝哥1 小时前
08.Spring AI 结合 Ollama:本地大模型应用开发实战
java·人工智能·spring
播播资源2 小时前
MiniMax-M4 API 选型指南:接入、思考参数与缓存避坑(含实测数据)
java·spring·缓存
ZealSinger20 小时前
Boot4.2升后MVC路径匹配策略怎么处理
mvc·升级踩坑·boot42·路径匹配
Thanks_ks21 小时前
单例模式与工厂模式精讲:设计模式的意图、实现与在 Java 项目中的实际应用
java·spring·单例模式·设计模式·工厂模式·线程安全·面向对象设计
CoderYanger21 小时前
Java EE 进阶:16. Spring原理(第二弹)
java·服务器·spring boot·后端·spring·面试·java-ee
Rain的Java大神之路1 天前
读懂 Spring 源码,从"能编译"开始:Spring 7.0.8 环境搭建 + 阅读方法全攻略
java·后端·spring
nice_lcj5201 天前
在 Spring Boot 里学会使用 RabbitMQ:从入门到实战
java·spring boot·spring·rabbitmq·java-rabbitmq
安易算力1 天前
大模型推理部署实战:vLLM与TensorRT-LLM配置参数深度解析
java·spring·vllm
风中芦苇啊1 天前
Spring AI 项目实战:从零搭建一个智能问答服务
java·人工智能·spring
萧瑟余晖1 天前
Spring Framework详解篇三:Spring依赖注入与循环依赖详解
java·后端·spring