springboot虚拟请求——测试

springboot虚拟请求

表现层测试

web环境模拟测试

在这里插入图片描述
在这里插入图片描述

虚拟请求状态匹配——执行状态的匹配

    @Test
    void testStatus(@Autowired MockMvc mvc) throws Exception {
//       //http://localhost:8080/books
        // 创建一个虚拟请求,当前访问的是books
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books1");
        ResultActions actions = mvc.perform(builder);

        //设定预期值,与真实值进行比较,成功测试通过,失败测试失败
        // 定义本次调用的预期值
        StatusResultMatchers status = MockMvcResultMatchers.status();
        // 预计本次调用时成功的状态200
        ResultMatcher ok = status.isOk();

        // 添加与机制到本次调用过程中进行匹配
        actions.andExpect(ok);
    }

虚拟请求状态匹配——执行内容的匹配

   @Test
    void testBody(@Autowired MockMvc mvc) throws Exception {
//       //http://localhost:8080/books
        // 创建一个虚拟请求,当前访问的是books
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
        ResultActions actions = mvc.perform(builder);

        //设定预期值,与真实值进行比较,成功测试通过,失败测试失败
        // 定义本次调用的预期值
        ContentResultMatchers content = MockMvcResultMatchers.content();
        ResultMatcher result = content.string("springboot");

        // 添加与机制到本次调用过程中进行匹配
        actions.andExpect(result);
    }

虚拟请求状态匹配——执行内容的匹配(json)

 @Test
    void testJson(@Autowired MockMvc mvc) throws Exception {
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
        ResultActions actions = mvc.perform(builder);

        //设定预期值,与真实值进行比较,成功测试通过,失败测试失败
        // 定义本次调用的预期值
        ContentResultMatchers content = MockMvcResultMatchers.content();
        ResultMatcher result = content.json("{\n" +
                "    \"id\": 1,\n" +
                "    \"name\": \"springBoot\",\n" +
                "    \"type\": \"springBoot\",\n" +
                "    \"description\": \"springBoot\"\n" +
                "}");

        // 添加与机制到本次调用过程中进行匹配
        actions.andExpect(result);
    }

在这里插入图片描述

这里面的东西都可以匹配

虚拟请求状态匹配——匹配响应头

@Test
    void testContentType(@Autowired MockMvc mvc) throws Exception {
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
        ResultActions actions = mvc.perform(builder);

        //设定预期值,与真实值进行比较,成功测试通过,失败测试失败
        // 定义本次调用的预期值
        HeaderResultMatchers header = MockMvcResultMatchers.header();

        ResultMatcher contentType = header.string("Content-Type","application/json");
        // 添加与机制到本次调用过程中进行匹配
        actions.andExpect(contentType);
    }

实际应用的时候,可以把这些都放在一起
例如,把这些组合一下:

@Test
    void testGetById(@Autowired MockMvc mvc) throws Exception {
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
        ResultActions actions = mvc.perform(builder);

        StatusResultMatchers status = MockMvcResultMatchers.status();
        ResultMatcher ok = status.isOk();
        actions.andExpect(ok);


        HeaderResultMatchers header = MockMvcResultMatchers.header();
        ResultMatcher contentType = header.string("Content-Type","application/json");
        actions.andExpect(contentType);


        ContentResultMatchers content = MockMvcResultMatchers.content();
        ResultMatcher result = content.json("{\n" +
                "    \"id\": 1,\n" +
                "    \"name\": \"springBoot\",\n" +
                "    \"type\": \"springBoot2\",\n" +
                "    \"description\": \"springBoot\"\n" +
                "}");
        actions.andExpect(result);
    }

数据层测试事务回滚

在这里插入图片描述

测试用例数据设定

在这里插入图片描述
配置一个对象来使用,比如:

package com.itheima.testcase.domain;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "testcase.book")
public class BookCase {
    private int id;
    private int id2;
    private int type;
    private String name;
    private  String uuid;
    private long publishTime;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值