目录
一 在测试类中启动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";
}
}
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);
}
}
总结
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);
}
}
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);
}
}
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);
}
}
正确情况
错误情况
总结
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);
}
}
错误情况
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);
}
}