SpringBoot請(qǐng)求發(fā)送與信息響應(yīng)匹配實(shí)現(xiàn)方法介紹
發(fā)送虛擬請(qǐng)求訪問controller
我們在test類中虛擬訪問controller,就得發(fā)送虛擬請(qǐng)求。
先創(chuàng)建一個(gè)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中 ,這個(gè)是一個(gè)get請(qǐng)求,所以我們調(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)用對(duì)象
public void test(@Autowired MockMvc mvc) throws Exception {
//創(chuàng)建虛擬請(qǐng)求,當(dāng)前訪問/tests,MockMvcRequestBuilders是一個(gè)工具類
MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/tests");
//執(zhí)行請(qǐng)求
mvc.perform(builder);
}
}訪問需要用到的一個(gè)RequestBuilder,我們按ctrl+h顯示出它的實(shí)現(xiàn)類

運(yù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)用對(duì)象
public void test(@Autowired MockMvc mvc) throws Exception {
//創(chuàng)建虛擬請(qǐng)求,當(dāng)前訪問/tests,MockMvcRequestBuilders是一個(gè)工具類
MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/tests");
// 執(zhí)行請(qǐng)求
ResultActions action = mvc.perform(builder);
//設(shè)置預(yù)期值與真實(shí)值進(jìn)行比較,成功則測試通過,失敗則測試不通過
//定義本次調(diào)用的預(yù)期值
StatusResultMatchers status= MockMvcResultMatchers.status();
//預(yù)計(jì)本次調(diào)用成功的狀態(tài)為200
ResultMatcher ok=status.isOk();
//添加預(yù)計(jì)值到本次調(diào)用過程中進(jìn)行匹配
action.andExpect(ok);
}
}運(yùn)行成功不會(huì)有任何反應(yīng)

當(dāng)將get改為put制造一個(gè)錯(cuò)誤,或修改不存在的路徑等其他錯(cuò)誤,則就會(huì)報(bào)出錯(cuò)誤信息。

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

匹配json格式響應(yīng)體
先創(chuàng)建一個(gè)類pojo對(duì)象
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;
}
}啟動(dòng)訪問得到一組json數(shù)據(jù)

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

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

