• SpringBoot SpringBoot 开发实用篇 3 测试 3.7 匹配响应体【JSON】


    SpringBoot

    【黑马程序员SpringBoot2全套视频教程,springboot零基础到项目实战(spring boot2完整版)】

    SpringBoot 开发实用篇

    3 测试

    3.7 匹配响应体【JSON】
    3.7.1 问题引入

    之前我们已经测试去匹配响应体,但是是个光的字符串,

    在这里插入图片描述

    但是很明显,我们以后不会只是匹配一个头铁的字符串这么简单,如果想匹配json 数据

    【怎么做?】

    3.7.2 匹配响应体【JSON】

    加一个lombok 依赖

    <dependency>
        <groupId>org.projectlombokgroupId>
        <artifactId>lombokartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    【创建一个Book 实体类】

    package com.dingjiaxiong.domain;
    
    import lombok.Data;
    
    /**
     * ClassName: Book
     * date: 2022/10/19 21:27
     *
     * @author DingJiaxiong
     */
    
    @Data
    public class Book {
        private int id;
        private String name;
        private String type;
        private String description;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    OK

    【改造controller】

    package com.dingjiaxiong.controller;
    
    import com.dingjiaxiong.domain.Book;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * ClassName: BookController
     * date: 2022/10/19 20:55
     *
     * @author DingJiaxiong
     */
    
    @RestController
    @RequestMapping("/books")
    public class BookController {
    
        @GetMapping
        public Book getById(){
            System.out.println("getById is running ...");
    
            //模拟数据
            Book book = new Book();
            book.setId(1);
            book.setName("计算机牛逼");
            book.setType("计算机");
            book.setDescription("黑马666");
    
            return book;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    试一下,看看是不是返回json 数据

    在这里插入图片描述

    OK,没毛病

    测试匹配

    package com.dingjiaxiong;
    
    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.RequestBuilder;
    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;
    import org.springframework.test.web.servlet.result.StatusResultMatchers;
    
    /**
     * ClassName: WebTest
     * date: 2022/10/19 20:29
     *
     * @author DingJiaxiong
     */
    
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    //1. 开启虚拟MVC调用
    @AutoConfigureMockMvc
    public class WebTest {
    
        @Test
        void testWeb(@Autowired MockMvc mvc) throws Exception {
    
            //创建一个虚拟请求,访问/books
            MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
            //执行请求
            ResultActions action = mvc.perform(builder);
    
            //定义本次调用的预期值
            ContentResultMatchers content = MockMvcResultMatchers.content();
            //预计本次调用是返回一个SpringBoot
            ResultMatcher json = content.json("{\"id\":1,\"name\":\"计算机牛逼\",\"type\":\"计算机\",\"description\":\"黑马666\"}");
    
            //添加预计值到本地调用过程中进行匹配
            action.andExpect(json);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    直接运行

    在这里插入图片描述

    给它改错

    在这里插入图片描述

    再试一次

    在这里插入图片描述

    厉害了

    【json 和字符串就只是获取的时候解析方式不同,其他没有区别】

    回顾一下

    在这里插入图片描述

  • 相关阅读:
    上线node项目
    破防了,原来这才是机房运维的正确方法
    The valid characters are defined in RFC 7230 and RFC 3986
    Spring - 注解整理
    MySQL表的约束
    A股一年见两次2800 那么期货是怎么多空操作的?
    【MySQL系列】- SELECT语句执行顺序
    不得不会的Oracle数据库知识点(二)
    Android 13.0 系统settings详情页卸载修改为停止,禁止卸载app功能实现
    SpringBoot对Filter过滤器中的异常进行全局处理
  • 原文地址:https://blog.csdn.net/weixin_44226181/article/details/127897142