SpringBoot請(qǐng)求發(fā)送與信息響應(yīng)匹配實(shí)現(xiàn)方法介紹
發(fā)送虛擬請(qǐng)求訪問(wèn)controller
我們?cè)趖est類中虛擬訪問(wèn)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) //開(kāi)啟虛擬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)前訪問(wèn)/tests,MockMvcRequestBuilders是一個(gè)工具類 MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/tests"); //執(zhí)行請(qǐng)求 mvc.perform(builder); } }
訪問(wèn)需要用到的一個(gè)RequestBuilder,我們按ctrl+h顯示出它的實(shí)現(xiàn)類
運(yùn)行結(jié)果
打印出了結(jié)果,說(shuō)明訪問(wèn)成功
匹配響應(yīng)執(zhí)行狀態(tài)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) //開(kāi)啟虛擬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)前訪問(wèn)/tests,MockMvcRequestBuilders是一個(gè)工具類 MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/tests"); // 執(zhí)行請(qǐng)求 ResultActions action = mvc.perform(builder); //設(shè)置預(yù)期值與真實(shí)值進(jìn)行比較,成功則測(cè)試通過(guò),失敗則測(cè)試不通過(guò) //定義本次調(diào)用的預(yù)期值 StatusResultMatchers status= MockMvcResultMatchers.status(); //預(yù)計(jì)本次調(diào)用成功的狀態(tài)為200 ResultMatcher ok=status.isOk(); //添加預(yù)計(jì)值到本次調(diào)用過(guò)程中進(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) //開(kāi)啟虛擬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)前訪問(wèn)/tests,MockMvcRequestBuilders是一個(gè)工具類 MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/tests"); // 執(zhí)行請(qǐng)求 ResultActions action = mvc.perform(builder); //設(shè)置預(yù)期值與真實(shí)值進(jìn)行比較,成功則測(cè)試通過(guò),失敗則測(cè)試不通過(guò) //定義本次調(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)用過(guò)程中進(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)訪問(wèn)得到一組json數(shù)據(jù)
我們?cè)跍y(cè)試類中修改一個(gè),使他產(chǎn)生錯(cuò)誤的信息
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) //開(kāi)啟虛擬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)前訪問(wèn)/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)用過(guò)程中進(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)用過(guò)程中進(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í)的問(wèn)題解決)
- springboot vue完成發(fā)送接口請(qǐng)求顯示響應(yīng)頭信息
- SpringBoot之自定義Filter獲取請(qǐng)求參數(shù)與響應(yīng)結(jié)果案例詳解
- SpringBoot請(qǐng)求響應(yīng)方式示例詳解
相關(guān)文章

解決SpringBoot加載application.properties配置文件的坑

java httpclient設(shè)置超時(shí)時(shí)間和代理的方法

JAVA中StringBuffer與String的區(qū)別解析

Java7之forkjoin簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

java中BeanNotOfRequiredTypeException的問(wèn)題解決(@Autowired和@Resourc

解決Request.getParameter獲取不到特殊字符bug問(wèn)題