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()) ;
        }
    }
相关推荐
li35745 分钟前
深入理解:MQ监听类 vs Spring事件监听类 —— 区别、用法与适用场景全解析
java·数据库·spring
Mr.朱鹏1 小时前
ShardingJDBC实战指南
java·jvm·数据库·spring·分库分表·shardingjdbc·shardingshere
麦兜*2 小时前
MongoDB 备份与恢复终极指南:mongodump 和 mongorestore 深度实战
java·数据库·spring boot·mongodb·spring
hello 早上好3 小时前
Spring MVC 类型转换与参数绑定:从架构到实战
spring·架构·mvc
用户6120414922133 小时前
springmvc做的学生考勤管理系统
javascript·后端·spring
珹洺4 小时前
Java-Spring入门指南(五)Spring自动装配
android·java·spring
minh_coo14 小时前
Spring框架事件驱动架构核心注解之@EventListener
java·后端·spring·架构·intellij-idea
sanggou14 小时前
License 集成 Spring Gateway:解决 WebFlux 非阻塞与 Spring MVC Servlet 阻塞兼容问题
spring·gateway·mvc
再睡亿分钟!16 小时前
Spring MVC 的常用注解
java·开发语言·spring boot·spring