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()) ;
        }
    }
相关推荐
麦兜*20 小时前
MongoDB Atlas 云数据库实战:从零搭建全球多节点集群
java·数据库·spring boot·mongodb·spring·spring cloud
麦兜*20 小时前
MongoDB 在物联网(IoT)中的应用:海量时序数据处理方案
java·数据库·spring boot·物联网·mongodb·spring
青衫客3621 小时前
Spring异步编程- 浅谈 Reactor 核心操作符
java·spring·响应式编程
Cyan_RA91 天前
SpringMVC @RequestMapping的使用演示和细节 详解
java·开发语言·后端·spring·mvc·ssm·springmvc
wuxuanok1 天前
SpringBoot -原理篇
java·spring boot·spring
若鱼19192 天前
spring-kafka消费异常处理
spring·kafka
送秋三十五2 天前
spring源码分析————ListableBeanFactory
java·后端·spring
一又四分之一.2 天前
spring、springboot、springCloud
spring boot·spring·spring cloud
float_六七2 天前
Spring事务注解@Transactional核心机制详解
java·后端·spring
Java水解2 天前
从 “Hello AI” 到企业级应用:Spring AI 如何重塑 Java 生态的 AI 开发
后端·spring