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()) ;
        }
    }
相关推荐
Mr.朱鹏4 小时前
针对Feign客户端请求体参数处理问题
java·jvm·spring boot·spring·spring cloud·maven·intellij-idea
涛粒子6 小时前
Spring Bean 生命周期的执行流程
java·后端·spring
quo-te7 小时前
【无标题】
java·spring·maven·mybatis·idea
_周游7 小时前
【Spring+MyBatis】_图书管理系统(上篇)
spring·oracle·mybatis
大龄码农有梦想11 小时前
Springboot集成Spring AI和Milvus,验证RAG构建过程
人工智能·spring boot·spring·milvus·知识库·rag·spring ai
caihuayuan415 小时前
mysql多主集群 galera cluster for mysql 8安装配置启动重启集群
java·大数据·sql·spring
程序猿零零漆16 小时前
SpringCloud系列教程:微服务的未来(二十四)Direct交换机、Topic交换机、声明队列交换机
spring·spring cloud·微服务·rabbitmq
2501_9032386516 小时前
Log4j在Spring项目中的应用与实践
java·spring·log4j·个人开发
半旧夜夏19 小时前
阿里云IOT消息处理
java·ide·git·物联网·spring·阿里云
冬天vs不冷20 小时前
深入理解Spring FactoryBean:灵活创建复杂对象的秘密武器
java·后端·spring