文章目录

  • 匹配响应执行状态
  • 案例模拟
  • 编写WebTest.java
  • 测试运行
  • 小结
  • 匹配响应体(了解)
  • 案例模拟
  • 编写WebTest.java
  • 测试运行
  • 小结
  • 匹配响应体(json)
  • 案例模拟
  • 创建User实体类
  • 编辑UserController.java
  • 运行测试
  • 编辑WebTest.java
  • 测试运行
  • 小结
  • 匹配响应头
  • 案例模拟
  • 编写WebTest.java
  • 运行测试
  • 小结
  • 总结如何使用测试响应


匹配响应执行状态

案例模拟

编写WebTest.java

更改urlTemplate为错误地址

package com.taotao;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.result.StatusResultMatchers;

/**
 * create by 刘鸿涛
 * 2022/5/21 19:41
 */
@SuppressWarnings({"all"})
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) //随机端口
@AutoConfigureMockMvc
public class WebTest {
    @Test
    void test(){

    }

    @Test
    void testWeb(@Autowired MockMvc mvc) throws Exception {
        //创建虚拟请求,当前访问/Users
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/Users1");
        //执行对应的请求
        ResultActions actions = mvc.perform(builder);

        //设定预期值 与真实值进行比较,成功测试通过,失败测试失败
        StatusResultMatchers status = MockMvcResultMatchers.status();
        //预计本次调用时成功的:状态200
        ResultMatcher ok = status.isOk();
        //添加预计值到本次调用过程中进行匹配
        actions.andExpect(ok);
    }
}

测试运行

springboot往request中添加header springboot添加响应头_spring

小结

springboot往request中添加header springboot添加响应头_java_02

匹配响应体(了解)

更改expectedContent属性为错误响应体

案例模拟

编写WebTest.java

package com.taotao;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.ContentResultMatchers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

/**
 * create by 刘鸿涛
 * 2022/5/21 19:41
 */
@SuppressWarnings({"all"})
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) //随机端口
@AutoConfigureMockMvc
public class WebTest {
    @Test
    void test(){

    }

    @Test
    void testBody(@Autowired MockMvc mvc) throws Exception {
        //创建虚拟请求,当前访问/Users
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/Users");
        //执行对应的请求
        ResultActions actions = mvc.perform(builder);
        //设定预期值 与真实值进行比较,成功测试通过,失败测试失败
        //定义本次的预期值
        ContentResultMatchers content = MockMvcResultMatchers.content();
        //预计本次调用时成功的:状态200
        ResultMatcher result = content.string("springboot1");
        //添加预计值到本次调用过程中进行匹配
        actions.andExpect(result);
    }
}

测试运行

springboot往request中添加header springboot添加响应头_spring boot_03

小结

springboot往request中添加header springboot添加响应头_spring_04

匹配响应体(json)

案例模拟

创建User实体类

package com.taotao.domain;

import lombok.Data;

/**
 * create by 刘鸿涛
 * 2022/5/22 12:19
 */
@SuppressWarnings({"all"})
@Data
public class User {
    private int id;
    private String name;
    private int age;
    private String  address;
}

编辑UserController.java

package com.taotao.controller;

import com.taotao.domain.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * create by 刘鸿涛
 * 2022/5/21 21:29
 */
@SuppressWarnings({"all"})
@RestController
@RequestMapping("/Users")
public class UserController {
    @GetMapping
    public User getById() {
        System.out.println("getById is running");
        User user = new User();
        user.setId(1);
        user.setName("涛涛");
        user.setAge(18);
        user.setAddress("河南");
        return user;
    }
}
运行测试

运行Springboot14TestApplication.java

springboot往request中添加header springboot添加响应头_json_05

编辑WebTest.java

返回的json是上面的测试通过返回的json数据

package com.taotao;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.ContentResultMatchers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

/**
 * create by 刘鸿涛
 * 2022/5/21 19:41
 */
@SuppressWarnings({"all"})
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) //随机端口
@AutoConfigureMockMvc
public class WebTest {
    @Test
    void test(){

    }

    @Test
    void testJosn(@Autowired MockMvc mvc) throws Exception {
        //创建虚拟请求,当前访问/Users
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/Users");
        //执行对应的请求
        ResultActions actions = mvc.perform(builder);
        //设定预期值 与真实值进行比较,成功测试通过,失败测试失败
        //定义本次的预期值
        ContentResultMatchers content = MockMvcResultMatchers.content();
        //预计本次调用时成功的:状态200
        ResultMatcher json = content.json("{\"id\":1,\"name\":\"" +
                "涛涛\",\"age\":18,\"address\":\"河南\"}");
        //添加预计值到本次调用过程中进行匹配
        actions.andExpect(json);
    }
}

测试运行

成功

springboot往request中添加header springboot添加响应头_json_06

更改错误的json串,测试运行

springboot往request中添加header springboot添加响应头_spring_07

springboot往request中添加header springboot添加响应头_json_08

小结

springboot往request中添加header springboot添加响应头_Test_09

匹配响应头

案例模拟

编写WebTest.java

String参数和value参数都在这

springboot往request中添加header springboot添加响应头_spring_10

package com.taotao;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.ContentResultMatchers;
import org.springframework.test.web.servlet.result.HeaderResultMatchers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

/**
 * create by 刘鸿涛
 * 2022/5/21 19:41
 */
@SuppressWarnings({"all"})
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) //随机端口
@AutoConfigureMockMvc
public class WebTest {
    @Test
    void test(){

    }

    @Test
    void testJosn(@Autowired MockMvc mvc) throws Exception {
        //创建虚拟请求,当前访问/Users
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/Users");
        //执行对应的请求
        ResultActions actions = mvc.perform(builder);
        //设定预期值 与真实值进行比较,成功测试通过,失败测试失败
        //定义本次的预期值
        HeaderResultMatchers header = MockMvcResultMatchers.header();
        //预计本次调用时成功的:状态200
        ResultMatcher string = header.string("Content-Type", "application/json");
        //添加预计值到本次调用过程中进行匹配
        actions.andExpect(string);
    }
}

运行测试

成功

springboot往request中添加header springboot添加响应头_spring_11

修改数据,失败

springboot往request中添加header springboot添加响应头_spring_12

springboot往request中添加header springboot添加响应头_java_13

小结

springboot往request中添加header springboot添加响应头_spring_14

springboot往request中添加header springboot添加响应头_json_15

总结如何使用测试响应

如果时间足够,所有测试写在一起,直接全测

springboot往request中添加header springboot添加响应头_json_16