前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot(十五):MockMVC-web单元测试

SpringBoot(十五):MockMVC-web单元测试

作者头像
低调小熊猫
发布2020-06-05 17:57:12
8870
发布2020-06-05 17:57:12
举报
文章被收录于专栏:低调小熊猫低调小熊猫

简介

开发一个优秀的系统,单元测试也是必不可少的,Spring Boot 对单元测试也做了一些支持,MockMVC就是之一,可以模拟web端的post,get请求,测试也能得到详细的过程

使用方法

添加依赖

代码语言:javascript
复制
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

编写测试代码

代码语言:javascript
复制
@SpringBootTest
public class Springboot13StarterTestApplicationTests {

    private MockMvc mockMvc;

    //初始化资源
    @Before
    public void setMockMvc() throws Exception{
        mockMvc= MockMvcBuilders.standaloneSetup(new HelloController()).build();
    }

    @Test
    public void test() throws Exception{
        mockMvc.perform(MockMvcRequestBuilders.post("/hello?name=低调小熊猫")
                .accept(MediaType.APPLICATION_JSON_UTF8)).andDo(print());
    }
    @Test
    public void test2() throws Exception{
        mockMvc.perform(MockMvcRequestBuilders.post("/hello?name=低调小熊猫")
                .accept(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(MockMvcResultMatchers.content().string(Matchers.containsString("低调小熊猫")));

    }
    @Test
    public void contextLoads() {
        System.out.println("低调小熊猫");
    }

}

代码作用accept(MediaType.APPLICATIONJSONUTF8)) 设置编码格式 andDo(print()) //会将请求和相应的过程都打印出来 Matchers.containsString("str"),判断返回的结果集中是否包含“str”这个字符串

运行测试

我们运行第一个test

代码语言:javascript
复制
MockHttpServletRequest:
      HTTP Method = POST
      Request URI = /hello
       Parameters = {name=[低调小熊猫]}
          Headers = {Accept=[application/json;charset=UTF-8]}
             Body = <no character encoding set>
    Session Attrs = {}

Handler:
             Type = com.hope.controller.HelloController
           Method = public java.lang.String com.hope.controller.HelloController.hello(java.lang.String)

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = null

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 200
    Error message = null
          Headers = {Content-Type=[application/json;charset=UTF-8], Content-Length=[21]}
     Content type = application/json;charset=UTF-8
             Body = 你好低调小熊猫
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

当看到“Body = 你好低调小熊猫”,表示成功了,还能看到整个请求详细信息

第二个test,会打印我们请求的结果

第三个测试,就是普通的测试了

以上代码只是spring-boot-starter-test 组件的一部分功能,还有很多好玩的一起学吧

源码

https://github.com/java-aodeng/hope

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

本文分享自 低调小熊猫 微信公众号,前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 简介
    • 使用方法
      • 运行测试
      • 源码
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档