Spring MVC单元测试笔记整理

使用@WebMvcTest注解
  1. 编写单元测试类
    @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. 编写配置类
    @Configuration
    @EnableWebMvc
    @ComponentScan(basePackages = "com.yicj.study.controller")
    public class WebMvcConfig {
        @Bean
        public HelloService helloService(){
            return new HelloServiceImpl() ;
        }
    }
    
  2. 编写单元测试
    @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. 编写单元测试
    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()) ;
        }
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值