SpringBoot发送虚拟请求~

1、创建一个测试用的TestController

@RestController
public class TestController {
    @GetMapping("/test")
    public String test() {
        System.out.println("TestController ... ");
        return "test";
    }
}

2、编写一个测试类用来发送虚拟请求

package com.qbb;

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;
import org.springframework.test.web.servlet.result.StatusResultMatchers;

/**
 * @author startqbb (个人博客:https://www.cnblogs.com/qbbit)
 * @date 2023-02-02  23:24
 * @tags 我爱的人在很远的地方, 我必须更加努力
 */
// 测试使用WEB环境启动
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
// 开启虚拟MVC调用
@AutoConfigureMockMvc
public class WebTest {

    /**
     * @param mockMvc 注入虚拟MockMvc对象
     */
    @Test
    public void test1(@Autowired MockMvc mockMvc){
        try {
            // 创建虚拟请求
            MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/test1");
            // 执行请求
            ResultActions perform = mockMvc.perform(builder);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @Test
    public void test2(@Autowired MockMvc mockMvc){
        try {
            // 创建虚拟请求
            MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/test1");
            // 执行请求
            ResultActions perform = mockMvc.perform(builder);

            // 设置预期值
            StatusResultMatchers status = MockMvcResultMatchers.status();
            ResultMatcher ok = status.isOk();

            // 预期值和真实值比较
            perform.andExpect(ok);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @Test
    public void testBody(@Autowired MockMvc mockMvc){
        try {
            // 创建虚拟请求
            MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/test");
            // 执行请求
            ResultActions perform = mockMvc.perform(builder);

            // 设置预期值
            ContentResultMatchers content = MockMvcResultMatchers.content();
            ResultMatcher body = content.string("test");

            // 预期值和真实值比较  响应体匹配
            perform.andExpect(body);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

结果

image

posted @   我也有梦想呀  阅读(84)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2022-02-02 Element-UI整合VUE下拉选项无法选中的一个小问题
点击右上角即可分享
微信分享提示