Spring?MVC中的Controller進行單元測試的實現
對Controller進行單元測試是Spring框架原生就支持的能力,它可以模擬HTTP客戶端發(fā)起對服務地址的請求,可以不用借助于諸如Postman這樣的外部工具就能完成對接口的測試。
具體來講,是由Spring框架中的spring-test
模塊提供的實現,詳見MockMvc。
如下將詳細闡述如何使用MockMvc測試框架實現對“Spring Controller”進行單元測試,基于Spring Boot開發(fā)框架進行驗證。
添加測試框架依賴:
<!-- Spring框架 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> !<-- Spring測試框架 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 文件操作工具 --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.11.0</version> </dependency>
導入靜態(tài)工具方法
為了便于在編寫測試用例時直接調用測試框架自帶的靜態(tài)方法,首先需要導入這些靜態(tài)工具方法。
需要導入的靜態(tài)方法如下:
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*; import static org.springframework.test.web.servlet.setup.SharedHttpSessionConfigurer.*;
初始化MockMvc
初始化MockMvc有2種方式:
方式1:明確指定需要測試的“Controller”類進行配置
方式2:基于Spring容器進行配置,包含了Spring MVC環(huán)境和所有“Controller”類,通常使用這種方式。
@SpringBootTest public class TestControllerTest { MockMvc mockMvc; // 初始化MockMvc @BeforeEach void setUp(WebApplicationContext wac) { // 方式1:明確指定需要測試的“Controller”類 this.mockMvc = MockMvcBuilders.standaloneSetup(new TestController()).build(); // 方式2:基于Spring容器進行配置,包含了Spring MVC環(huán)境和所有“Controller”類。 this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); } }
另外,還可以對MockMvc進行全局配置。
// 全局配置MockMvc this.mockMvc = MockMvcBuilders.webAppContextSetup(wac) .defaultRequest(get("/").accept(MediaType.APPLICATION_JSON)) // 默認請求路徑 .apply(sharedHttpSession()) // 配置session .alwaysExpect(status().isOk()) // 預期響應狀態(tài)碼 .alwaysExpect(content().contentType("application/json;charset=UTF-8")) // 預期內容類型 .build();
執(zhí)行測試
MockMvc支持對常見的HTTP方法,如:GET,POST,PUT,DELETE等,甚至還支持文件上傳請求。
測試GET接口
// 訪問GET接口:不帶參數 @Test public void testSimpleGet() throws Exception { MvcResult result = this.mockMvc.perform(get("/test/simple/get") .accept(MediaType.APPLICATION_JSON)) // 接受JSON格式響應消息 .andReturn(); // 獲取返回結果 Assertions.assertEquals("OK", result.getResponse().getContentAsString()); } // 訪問GET接口:帶URL參數 @Test public void testParamGet() throws Exception { int id = 10; // 方式1:在URI模板中指定參數 //MvcResult result = this.mockMvc.perform(get("/test/param/get?id={id}", id).accept(MediaType.APPLICATION_JSON)).andReturn(); // 方式2:通過param()方法指定參數 //MvcResult result = this.mockMvc.perform(get("/test/param/get").param("id", String.valueOf(id)).accept(MediaType.APPLICATION_JSON)).andReturn(); // 方式3:通過queryParam()方法指定參數 MvcResult result = this.mockMvc.perform(get("/test/param/get").queryParam("id", String.valueOf(id)).accept(MediaType.APPLICATION_JSON)).andReturn(); Assertions.assertEquals("OK: " + id, result.getResponse().getContentAsString()); }
測試POST接口
// 傳遞表單參數 @Test public void testSimplePost() throws Exception { int id = 10; // 調用param()方法傳遞參數 MvcResult result = this.mockMvc.perform(post("/test/simple/post") .param("id", String.valueOf(id)) .contentType(MediaType.APPLICATION_FORM_URLENCODED) .accept(MediaType.APPLICATION_JSON)) .andReturn(); Assertions.assertEquals("{\"id\":10}", result.getResponse().getContentAsString()); } // 傳遞JSON參數 @Test public void testSimplePostJson() throws Exception { // 調用content()方法傳遞json字符串參數 Subject subject = new Subject(); subject.setId(10); String content = JSON.toJSONString(subject); MvcResult result = this.mockMvc.perform(post("/test/simple/post/json") .content(content) .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON)) .andReturn(); Assertions.assertEquals("{\"id\":10}", result.getResponse().getContentAsString()); }
測試文件上傳
@Test public void testFileUploadSingle() throws Exception { File file = new File("C:\\Users\\xxx\\Downloads\\test.jpg"); String fileName = FilenameUtils.getName(file.getName()); byte[] bytes = FileUtils.readFileToByteArray(file); MockMultipartFile mockMultipartFile = new MockMultipartFile("file", fileName, MediaType.MULTIPART_FORM_DATA_VALUE, bytes); this.mockMvc.perform(multipart("/test/upload/single").file(mockMultipartFile)) .andExpect(status().isOk()) .andExpect(content().string("OK")) .andDo(print()); }
定義預期結果
斷言響應結果時,有2種方式:
1.使用JUnit提供的Assert斷言工具判斷返回結果,這是一種非常普遍和常見的方式
2.在MockMvc框架中可以通過andExpect()
方法定義一個或多個預期結果,當其中一個期望結果斷言失敗時,就不會斷言其他期望值了
// 使用Junit斷言工具判斷返回結果是否符合預期 @Test public void testAssertResult() throws Exception { MvcResult result = this.mockMvc.perform(get("/test/simple/get").accept(MediaType.APPLICATION_JSON)).andDo(print()).andReturn(); Assert.assertEquals("OK", result.getResponse().getContentAsString()); } // 在MockMvc框架中定義預期結果 @Test public void testExpectations() throws Exception { this.mockMvc.perform(get("/test/simple/get").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) // 預期響應狀態(tài)碼為200 .andExpect(content().string("OK")) // 預期返回值為字符串“OK” .andDo(print()); }
相比于使用Junit的斷言工具判斷返回結果,在MockMvc框架中直接定義預期結果進行斷言檢查更加簡潔。
寫在最后
使用Spring提供的測試框架MockMvc可以非常方便地實現對HTTP服務接口進行單元測試,不要把基礎的功能驗證工作都交給測試童鞋,應該通過單元測試來保證代碼迭代的穩(wěn)定性。
到此這篇關于Spring MVC中的Controller進行單元測試的實現的文章就介紹到這了,更多相關Spring MVC Controller單元測試內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Idea如何配置Maven才能優(yōu)先從本地倉庫獲取依賴(親測方法有效)
對于Idea怎么配置Maven才能優(yōu)先從本地倉庫獲取依賴,網上說法有很多種,都不太靠譜,最終都沒有效果,最好的解決方法是通過修改maven配置文件settings.xml,本文給大家介紹的非常詳細,需要的朋友參考下吧2023-10-10Java Swing中的JButton、JComboBox、JList和JColorChooser組件使用案例
這篇文章主要介紹了Java Swing中的按鈕(JButton)、組合框(JComboBox)、下拉列表(JList)和顏色選擇器(JColorChooser)組件使用案例,需要的朋友可以參考下2014-10-10