前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WebMvcTest和MockBean测试Controller

WebMvcTest和MockBean测试Controller

作者头像
Antony
发布2020-12-02 10:22:38
1.5K0
发布2020-12-02 10:22:38
举报

@WebMvcTest注解简介

在之前的案例中,笔者简单介绍了如何使用MockMvc配合Mockito来实现Controller的单元测试。

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

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

以下是这个注解的定义,具体可以参见 spring.factories

代码语言:javascript
复制
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的测试。还是借用之前TestLink4J作为案例。

案例1-单个Controller的测试

代码语言:javascript
复制
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-请求格式校验

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

代码语言:javascript
复制
(@RequestParam(value = "id", required = true)

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

代码语言:javascript
复制
    @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引入的各层进行单测的注解

代码语言:javascript
复制
@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,后续我们会继续涵盖其它注解。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-11-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 软件测试那些事 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • @WebMvcTest注解简介
  • 案例1-单个Controller的测试
  • 案例2-请求格式校验
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档