WebMvcTest和MockBean测试Controller

2020-11-05  本文已影响0人  antony已经被占用

@WebMvcTest注解简介

Spring框架提供了@WebMvcTest这一注解来配置Controller的上下文环境,以帮助实现对Controller层的测试。从中可以看出,

  1. 只实例化Controller。默认实例化所有的Controller,也可以指定只实例化某一到多个Controller。
  2. 会实例化一个MockMvc的bean,用于模拟收发http请求和响应。
  3. 默认搜索@SpringBootConfiguration注解的类作为配置类。(这里坑最多)
  4. 通过properties 指定运行时的参数
    具体可以参见 spring.factories

以下是这个注解的定义,

package org.springframework.boot.test.autoconfigure.web.servlet;

//import 
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@BootstrapWith(WebMvcTestContextBootstrapper.class)
@ExtendWith({SpringExtension.class})
@OverrideAutoConfiguration(
    enabled = false
)
@TypeExcludeFilters({WebMvcTypeExcludeFilter.class})
@AutoConfigureCache
@AutoConfigureWebMvc
@AutoConfigureMockMvc
@ImportAutoConfiguration
public @interface WebMvcTest {
    String[] properties() default {};

    @AliasFor("controllers")
    Class<?>[] value() default {};

    @AliasFor("value")
    Class<?>[] controllers() default {};

    boolean useDefaultFilters() default true;

    Filter[] includeFilters() default {};

    Filter[] excludeFilters() default {};

    @AliasFor(
        annotation = ImportAutoConfiguration.class,
        attribute = "exclude"
    )
    Class<?>[] excludeAutoConfiguration() default {};
}

接下来看一下,如何通过@WebMvcTest搭配MockMvc和@MockBean来进行单个Controller的测试

案例1-单个Controller的测试

package com.testlink4j.controller;
//import 

@WebMvcTest(KeywordsRestController.class)
public class KeywordsControllerMockBean2Test {
        @MockBean
        private KeywordsService keywordsServic;
        @Autowired
    private  MockMvc mockMvc;

  @Test
  public void CreateKeywordsSuccessfullyTest() throws Exception {
      Keywords keywords=Keywords.builder().id(666).keyword("tester").testproject_id(333).notes("tester").build();
      Mockito.when(keywordsServic.findKeywordById(1)).thenReturn(keywords);
      String responseString = mockMvc.perform(
              get("/api/keywords?id=1")    //请求的url,请求的方法是Post
                      .contentType(MediaType.APPLICATION_JSON)  //数据的格式
              // .content(jsonString)       //body
      ).andExpect(status().isOk())    //返回的状态是200
              .andDo(print())         //打印出请求和相应的内容
              .andReturn().getResponse().getContentAsString();   //将相应的数据转换为字符串
      assertThatJson(responseString).isEqualTo(keywords);
            assertThatJson(responseString).isEqualTo(keywords);
  }
}

以上代码实现了
1在Sping容器中只注入指定的KeywordsRestController,
2并通过@MockBean将该Controller对Service的依赖进行了mock 。在测试时,
3通过MockMvc模拟发送HTTP GET请求,并对http响应的状态码进行了验证,
4对返回的结果也进行了验证。

案例2-请求格式校验

再编写一个针对入参的校验用例

(@RequestParam(value = "id", required = true) 

也就是说,id这个字段是必填项

    @Test
    public void CreateKeywordsNoIdProvidedTest() throws UnsupportedEncodingException, Exception {
        mockMvc.perform(
                get("/api/keywords?idd=1")    //请求的url,请求的方法是Get
                        .contentType(MediaType.APPLICATION_JSON)  //数据的格式
                // .content(jsonString)       //body
        ).andExpect(status().is(400))    //返回的状态是400
                .andDo(print());       //打印出请求和相应的内容
    }

}

以上用例假设因为笔误,id错写成了idd,用例预期HTTP状态码会从200变成400 Bad Request ,即客户端错误。

以下是Spring1.4引入的各层进行单测的注解

@WebMvcTest - for testing the controller layer
@JsonTest - for testing the JSON marshalling and unmarshalling
@DataJpaTest - for testing the repository layer
@RestClientTests - for testing REST clients

本次主要介绍了@WebMvcTest,后续我们会继续涵盖其它注解。

By 软-件-测试-那-些 事

上一篇 下一篇

猜你喜欢

热点阅读