• 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()) ;
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
    使用@SpringJUnitWebConfig注解
    1. 编写配置类
      @Configuration
      @EnableWebMvc
      @ComponentScan(basePackages = "com.yicj.study.controller")
      public class WebMvcConfig {
          @Bean
          public HelloService helloService(){
              return new HelloServiceImpl() ;
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
    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()) ;
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
    使用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()) ;
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
  • 相关阅读:
    解决windows termail中文乱码的问题
    吴恩达2022机器学习专项课程C2W2:实验SoftMax
    sql第二条数据的某列更新到 第一列的某个字段里
    谈谈对mqtt和kafka的理解
    tomcat下载搭建
    Flutter自动路由插件auto_route详解
    【路径规划】基于遗传算法求解多车多类型车辆的车辆路径优化问题附matlab代码
    LLM之幻觉(一):大语言模型幻觉解决方案综述
    算法分析基础
    详解升讯威在线客服系统前端多国语言实现技术:原生支持葡文、印尼文、土耳其文、俄文
  • 原文地址:https://blog.csdn.net/yichengjie_c/article/details/133683746