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

栏目: Java · 发布时间: 7年前

内容简介:个人博客:https://aodeng.cc微信公众号:低调小熊猫QQ群:756796932

版权声明

单纯的广告

个人博客:https://aodeng.cc

微信公众号:低调小熊猫

QQ群:756796932

简介

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

使用方法

添加依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

编写测试代码

@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.APPLICATION_JSON_UTF8)) 设置编码格式

andDo(print()) //会将请求和相应的过程都打印出来

Matchers.containsString("str"),判断返回的结果集中是否包含“str”这个字符串

运行测试

我们运行第一个test

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

这只是我个人的学习笔记,非教程


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

创业36条军规

创业36条军规

孙陶然 / 中信出版社 / 2011-12 / 39.00元

《创业36军规》的作者孙陶然是一位数次成功创业的创业者,书中的内容有关创业的方方面面,从创业目的到股东选择,从经营到管理,从找方向到项目细节不一而足,写给每位心怀创业理想或正在创业路上的读者。 很多教人成才的书,作者未必成才;很多教人炒股的书,作者并不炒股;很多教人创业的书,作者不曾成功创业。一起来看看 《创业36条军规》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具