SpringBoot测试类中启动Web环境

目录

一 在测试类中启动Web环境

二 开始虚拟匹配

 三 不同的匹配

3.1 匹配响应执行状态

3.2 匹配响应体

3.3 匹配响应体(json)

 3.4 匹配响应头

 3.5 合


web项目中
准备工作:写一个controller

package com.qing.controller;

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/books")
public class BookController {

    @GetMapping
    public String getById(){
        System.out.println("getById is OK");
        return "springboot";
    }
}

一 在测试类中启动Web环境

package com.qing;

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.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

//以随机端口启动web
//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//以默认端口启动web环境
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
//开启虚拟MVC的调用
@AutoConfigureMockMvc
public class WebTest {

    //测试端口
    @Test
    public void testPort(){

    }
}

 结果

二 开始虚拟匹配

package com.qing;

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.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

//以随机端口启动web
//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//以默认端口启动web环境
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
//开启虚拟MVC的调用
@AutoConfigureMockMvc
public class WebTest {
  

    //开始虚拟匹配
    @Test
    public void testWeb(@Autowired MockMvc mvc) throws Exception {

        //模拟了虚拟的get请求,当前访问了/books
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
        //执行对应的请求
        mvc.perform(builder);
    }


}

 总结

 三 不同的匹配

3.1 匹配响应执行状态

package com.qing;

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;

//以随机端口启动web
//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//以默认端口启动web环境
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
//开启虚拟MVC的调用
@AutoConfigureMockMvc
public class WebTest {


    //方式1:匹配响应执行状态
    @Test
    public void testStatus(@Autowired MockMvc mvc) throws Exception {

        //模拟了虚拟的get请求,当前访问了/books
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books1");
        //执行对应的请求,返回真实的结果
        ResultActions perform = mvc.perform(builder);

        //设定预期值,与真实值进行比较
        StatusResultMatchers status = MockMvcResultMatchers.status();
        //预计本次调用成功,状态200
        ResultMatcher ok = status.isOk();
        //真实结果和预期结果进行比较
        perform.andExpect(ok);

    }

   

}

3.2 匹配响应体

package com.qing;

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;

//以随机端口启动web
//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//以默认端口启动web环境
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
//开启虚拟MVC的调用
@AutoConfigureMockMvc
public class WebTest {

    //方式2:匹配响应体
    @Test
    public void testBody(@Autowired MockMvc mvc) throws Exception {

        //模拟了虚拟的get请求,当前访问了/books
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
        //执行对应的请求,返回真实的结果
        ResultActions perform = mvc.perform(builder);

        //设定预期值,与真实值进行比较
        ContentResultMatchers content = MockMvcResultMatchers.content();
        //预计本次得到"springboot"的返回结果(controller里return的结果)
        ResultMatcher springboot = content.string("springboot2");
        //真实结果和预期结果进行比较
        perform.andExpect(springboot);

    }


}

 

3.3 匹配响应体(json)

package com.qing;

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;

//以随机端口启动web
//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//以默认端口启动web环境
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
//开启虚拟MVC的调用
@AutoConfigureMockMvc
public class WebTest {

    //方式3:匹配响应体(Json)
    @Test
    public void testBodyJson(@Autowired MockMvc mvc) throws Exception {

        //模拟了虚拟的get请求,当前访问了/books
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
        //执行对应的请求,返回真实的结果
        ResultActions perform = mvc.perform(builder);

        //设定预期值,与真实值进行比较
        ContentResultMatchers content = MockMvcResultMatchers.content();
        //预计本次得到"springboot"的返回结果(controller里return的结果)
        ResultMatcher springboot = content.json("{\"id\":1,\"name\":\"写文章的技巧\",\"type\":\"工具书\",\"description\":\"用它\"}");
        //真实结果和预期结果进行比较
        perform.andExpect(springboot);

    }



}

 此处修改了controller

 正确情况

 错误情况

 总结

 3.4 匹配响应头

package com.qing;

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;

//以随机端口启动web
//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//以默认端口启动web环境
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
//开启虚拟MVC的调用
@AutoConfigureMockMvc
public class WebTest {

 
    //方式4:匹配响应头
    @Test
    public void testContenType(@Autowired MockMvc mvc) throws Exception {

        //模拟了虚拟的get请求,当前访问了/books
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
        //执行对应的请求,返回真实的结果
        ResultActions perform = mvc.perform(builder);

        //设定预期值,与真实值进行比较
        HeaderResultMatchers header = MockMvcResultMatchers.header();
        //预计本次得到"springboot"的返回结果(controller里return的结果)
        ResultMatcher string = header.string("Content-Type", "application/json");
        //真实结果和预期结果进行比较
        perform.andExpect(string);

    }


}

 错误情况

 

 3.5 合

package com.qing;

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

//以随机端口启动web
//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//以默认端口启动web环境
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
//开启虚拟MVC的调用
@AutoConfigureMockMvc
public class WebTest {

    //合
    @Test
    public void testGetById(@Autowired MockMvc mvc) throws Exception {

        //模拟了虚拟的get请求,当前访问了/books
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
        //执行对应的请求,返回真实的结果
        ResultActions perform = mvc.perform(builder);


        //1 看status
        //设定预期值,与真实值进行比较
        StatusResultMatchers status = MockMvcResultMatchers.status();
        //预计本次调用成功,状态200
        ResultMatcher ok = status.isOk();
        //真实结果和预期结果进行比较
        perform.andExpect(ok);


        //2 看请求体
        //设定预期值,与真实值进行比较
        ContentResultMatchers content = MockMvcResultMatchers.content();
        //预计本次得到"springboot"的返回结果(controller里return的结果)
        ResultMatcher springboot = content.json("{\"id\":1,\"name\":\"写文章的技巧\",\"type\":\"工具书\",\"description\":\"用它\"}");
        //真实结果和预期结果进行比较
        perform.andExpect(springboot);


        //3 看请求头
        //设定预期值,与真实值进行比较
        HeaderResultMatchers header = MockMvcResultMatchers.header();
        //预计本次得到"springboot"的返回结果(controller里return的结果)
        ResultMatcher string = header.string("Content-Type", "application/json");
        //真实结果和预期结果进行比较
        perform.andExpect(string);

    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值