SpringBoot請求發(fā)送與信息響應(yīng)匹配實現(xiàn)方法介紹
發(fā)送虛擬請求訪問controller
我們在test類中虛擬訪問controller,就得發(fā)送虛擬請求。
先創(chuàng)建一個controller
package com.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/tests")
public class TestController {
@GetMapping
public String test(){
System.out.println("test is running");
return "test is success";
}
}在test中 ,這個是一個get請求,所以我們調(diào)用get,如果是put,則調(diào)用put即可
package com;
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.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//開啟虛擬MVC調(diào)用
@AutoConfigureMockMvc
public class WebTest {
@Test
// 注入虛擬MVC調(diào)用對象
public void test(@Autowired MockMvc mvc) throws Exception {
//創(chuàng)建虛擬請求,當前訪問/tests,MockMvcRequestBuilders是一個工具類
MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/tests");
//執(zhí)行請求
mvc.perform(builder);
}
}訪問需要用到的一個RequestBuilder,我們按ctrl+h顯示出它的實現(xiàn)類

運行結(jié)果

打印出了結(jié)果,說明訪問成功
匹配響應(yīng)執(zhí)行狀態(tài)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//開啟虛擬MVC調(diào)用
@AutoConfigureMockMvc
public class WebTest {
@Test
// 注入虛擬MVC調(diào)用對象
public void test(@Autowired MockMvc mvc) throws Exception {
//創(chuàng)建虛擬請求,當前訪問/tests,MockMvcRequestBuilders是一個工具類
MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/tests");
// 執(zhí)行請求
ResultActions action = mvc.perform(builder);
//設(shè)置預(yù)期值與真實值進行比較,成功則測試通過,失敗則測試不通過
//定義本次調(diào)用的預(yù)期值
StatusResultMatchers status= MockMvcResultMatchers.status();
//預(yù)計本次調(diào)用成功的狀態(tài)為200
ResultMatcher ok=status.isOk();
//添加預(yù)計值到本次調(diào)用過程中進行匹配
action.andExpect(ok);
}
}運行成功不會有任何反應(yīng)

當將get改為put制造一個錯誤,或修改不存在的路徑等其他錯誤,則就會報出錯誤信息。

匹配響應(yīng)體
虛擬請求體匹配
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//開啟虛擬MVC調(diào)用
@AutoConfigureMockMvc
public class WebTest {
@Test
// 注入虛擬MVC調(diào)用對象
public void testBody(@Autowired MockMvc mvc) throws Exception {
//創(chuàng)建虛擬請求,當前訪問/tests,MockMvcRequestBuilders是一個工具類
MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/tests");
// 執(zhí)行請求
ResultActions action = mvc.perform(builder);
//設(shè)置預(yù)期值與真實值進行比較,成功則測試通過,失敗則測試不通過
//定義本次調(diào)用的預(yù)期值
ContentResultMatchers content = MockMvcResultMatchers.content();
//預(yù)計本次調(diào)用成功的狀態(tài)為200
ResultMatcher result= content.string("test is success1");
//添加預(yù)計值到本次調(diào)用過程中進行匹配
action.andExpect(result);
}
}如果一致則不會有任何錯誤信息出現(xiàn), 若信息不一致,則會出現(xiàn)

匹配json格式響應(yīng)體
先創(chuàng)建一個類pojo對象
package com.pojo;
import lombok.Data;
@Data
public class Person {
private String name;
private String age;
private String detail;
}controller下
package com.controller;
import com.pojo.Person;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/tests")
public class TestController {
@RequestMapping("/person")
public Person testPerson(){
Person person = new Person();
person.setName("zhangsan");
person.setAge("14");
person.setDetail("xijie");
return person;
}
}啟動訪問得到一組json數(shù)據(jù)

我們在測試類中修改一個,使他產(chǎn)生錯誤的信息
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//開啟虛擬MVC調(diào)用
@AutoConfigureMockMvc
public class WebTest
@Test
// 注入虛擬MVC調(diào)用對象
public void testJson(@Autowired MockMvc mvc) throws Exception {
//創(chuàng)建虛擬請求,當前訪問/tests,MockMvcRequestBuilders是一個工具類
MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/tests/person");
// 執(zhí)行請求
ResultActions action = mvc.perform(builder);
ContentResultMatchers content = MockMvcResultMatchers.content();
ResultMatcher result= content.json("{\"name\":\"zhangsan\",\"age\":\"14\",\"detail\":\"xijie1\"}");
//添加預(yù)計值到本次調(diào)用過程中進行匹配
action.andExpect(result);
}
}運行結(jié)果

匹配響應(yīng)頭
@Test
// 注入虛擬MVC調(diào)用對象
public void testHeader(@Autowired MockMvc mvc) throws Exception {
MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/tests");
ResultActions action = mvc.perform(builder);
HeaderResultMatchers header = MockMvcResultMatchers.header();
ResultMatcher result = header.string("Content-Type", "application/json");
//添加預(yù)計值到本次調(diào)用過程中進行匹配
action.andExpect(result);
}匹配了一個/tests,返回字符串的方法。,就可以看出它的差別了
@RestController
@RequestMapping("/tests")
public class TestController {
@GetMapping
public String test(){
System.out.println("test is running");
return "test is success";
}
}
一般的做法都是將這些寫在同一方法。
到此這篇關(guān)于SpringBoot請求發(fā)送與信息響應(yīng)匹配實現(xiàn)方法介紹的文章就介紹到這了,更多相關(guān)SpringBoot請求發(fā)送內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實現(xiàn)的進制轉(zhuǎn)換工具類完整示例
這篇文章主要介紹了Java實現(xiàn)的進制轉(zhuǎn)換工具類,結(jié)合完整實例形式分析了Java實現(xiàn)二進制、十六進制、字符串、數(shù)組等相關(guān)轉(zhuǎn)換操作技巧,需要的朋友可以參考下2018-07-07
Java基礎(chǔ)之Comparable與Comparator概述
這篇文章主要介紹了Java基礎(chǔ)之Comparable與Comparator詳解,文中有非常詳細的代碼示例,對正在學習java基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
Spring Boot連接超時導致502錯誤的實戰(zhàn)案例
這篇文章主要給大家介紹了關(guān)于Spring Boot連接超時導致502錯誤的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09
idea2020.1設(shè)置多個spring boot的service啟動的實現(xiàn)
這篇文章主要介紹了idea2020.1設(shè)置多個spring boot的service啟動,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06
Java 實戰(zhàn)項目之誠途旅游系統(tǒng)的實現(xiàn)流程
讀萬卷書不如行萬里路,只學書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SpringBoot+Vue+maven+Mysql實現(xiàn)一個精美的物流管理系統(tǒng),大家可以在過程中查缺補漏,提升水平2021-11-11

