SpringBoot請求發(fā)送與信息響應匹配實現(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)類
運行結果
打印出了結果,說明訪問成功
匹配響應執(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); //設置預期值與真實值進行比較,成功則測試通過,失敗則測試不通過 //定義本次調(diào)用的預期值 StatusResultMatchers status= MockMvcResultMatchers.status(); //預計本次調(diào)用成功的狀態(tài)為200 ResultMatcher ok=status.isOk(); //添加預計值到本次調(diào)用過程中進行匹配 action.andExpect(ok); } }
運行成功不會有任何反應
當將get改為put制造一個錯誤,或修改不存在的路徑等其他錯誤,則就會報出錯誤信息。
匹配響應體
虛擬請求體匹配
@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); //設置預期值與真實值進行比較,成功則測試通過,失敗則測試不通過 //定義本次調(diào)用的預期值 ContentResultMatchers content = MockMvcResultMatchers.content(); //預計本次調(diào)用成功的狀態(tài)為200 ResultMatcher result= content.string("test is success1"); //添加預計值到本次調(diào)用過程中進行匹配 action.andExpect(result); } }
如果一致則不會有任何錯誤信息出現(xiàn), 若信息不一致,則會出現(xiàn)
匹配json格式響應體
先創(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\"}"); //添加預計值到本次調(diào)用過程中進行匹配 action.andExpect(result); } }
運行結果
匹配響應頭
@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"); //添加預計值到本次調(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"; } }
一般的做法都是將這些寫在同一方法。
到此這篇關于SpringBoot請求發(fā)送與信息響應匹配實現(xiàn)方法介紹的文章就介紹到這了,更多相關SpringBoot請求發(fā)送內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

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

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

Java7之forkjoin簡介_動力節(jié)點Java學院整理

java中BeanNotOfRequiredTypeException的問題解決(@Autowired和@Resourc

解決Request.getParameter獲取不到特殊字符bug問題