SpringBoot开发实用篇 - 测试 - 匹配响应执行状态,匹配响应体(json版),匹配响应头

匹配响应执行状态

案例模拟

编写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);
    }
}

测试运行

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KRCtaL4D-1653194588625)(springboot.assets/image-20220522112744438.png)]

小结

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4B4EtBrv-1653194588626)(springboot.assets/image-20220522112934281.png)]

匹配响应体(了解)

更改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);
    }
}

测试运行

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-n2VK8eUt-1653194588627)(springboot.assets/image-20220522121540460.png)]

小结

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qHCi1Xzv-1653194588628)(springboot.assets/image-20220522121607220.png)]

匹配响应体(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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5SMNJzSs-1653194588629)(springboot.assets/image-20220522122532960.png)]

编辑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);
    }
}

测试运行

成功

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-O6t4YqEZ-1653194588630)(springboot.assets/image-20220522122915826.png)]

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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VPMl7AUS-1653194588631)(springboot.assets/image-20220522122945341.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PMwnFcuM-1653194588632)(springboot.assets/image-20220522123018798.png)]

小结

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sKxgXYkL-1653194588632)(springboot.assets/image-20220522123126783.png)]

匹配响应头

案例模拟

编写WebTest.java

String参数和value参数都在这

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hUONQ3xi-1653194588633)(springboot.assets/image-20220522123546525.png)]

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);
    }
}

运行测试

成功

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yNOYiTzj-1653194588633)(springboot.assets/image-20220522123647823.png)]

修改数据,失败

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IFld4JSQ-1653194588634)(springboot.assets/image-20220522123710865.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ydov4uwO-1653194588634)(springboot.assets/image-20220522123748676.png)]

小结

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wmhYbbzw-1653194588635)(springboot.assets/image-20220522124156519.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wRdp76O3-1653194588635)(springboot.assets/image-20220522124204571.png)]

总结如何使用测试响应

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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eWQDTTVL-1653194588636)(springboot.assets/image-20220522124019918.png)]

猜你喜欢

转载自blog.csdn.net/qq_39123467/article/details/124909193