SpringBoot Controller Post接口單元測(cè)試示例
概述
在日常的開(kāi)發(fā)中,我們一般會(huì)定義一個(gè)service
層,用于實(shí)現(xiàn)業(yè)務(wù)邏輯,并且針對(duì)service
層會(huì)有與之對(duì)應(yīng)的齊全的覆蓋率高的單元測(cè)試。而對(duì)于controller
層,一般不怎么做單元測(cè)試,因?yàn)橹饕暮诵臉I(yè)務(wù)邏輯都在service
層里,controller
層只是做轉(zhuǎn)發(fā),調(diào)用service
層接口而已。但是還是建議使用單元測(cè)試簡(jiǎn)單的將controller
的方法跑一下,看看轉(zhuǎn)發(fā)和數(shù)據(jù)轉(zhuǎn)換的代碼是否能正常工作。
在Spring Boot
里對(duì)controller
層進(jìn)行單元測(cè)試非常簡(jiǎn)單,只需要幾個(gè)注解和一點(diǎn)點(diǎn)輔助代碼即可搞定。
依賴(lài)的包
<dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> </dependency>
使用的Spring Boot 版本
2.0.4.RELEASE
代碼
@ExtendWith(SpringExtension.class) @SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.MOCK,classes = TestApplication.class) @AutoConfigureMockMvc public class UserControllerTest { @Autowired private MockMvc mockMvc; @MockBean private UserService userService; @Test @DisplayName("測(cè)試controller方法") void test() throws Exception { User param = new User(); param.setUserId(1111); List<Address> addressList = new ArrayList<>(); Address address = new Address(); address.setName("我的地址"); addressList.add(address); param.setAddressList(addressList); MvcResult mvcResult = mockMvc.perform( post("/xxx/test") .contentType(MediaType.APPLICATION_JSON) .content(JSON.toJSONString(param))) .andReturn(); System.out.println(mvcResult.getResponse().getContentAsString()); } }
@RequestMapping(value = "/xxx", method = RequestMethod.POST) public Object test(@RequestBody(required = false)User user) throws Exception { }
如果你只是想簡(jiǎn)單的跑一下controller
層,不想真正的去執(zhí)行service
方法的話,需要使用@MockBean
將對(duì)應(yīng)的service
類(lèi)mock
掉。
@MockBean private UserService userService;
使用Spring Boot Test
的時(shí)候,它需要一個(gè)ApplicationContext
,我們可以在@SpringBootTest
注解中使用classes
屬性來(lái)指定。
@SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.MOCK,classes = TestApplication.class)
TestApplication
的代碼很簡(jiǎn)單。
@SpringBootApplication public class TestApplication { public static void main(String[] args){ SpringApplicationBuilder builder = new SpringApplicationBuilder(); builder.environment(new StandardEnvironment()); builder.sources(TestApplication.class); builder.main(TestApplication.class); builder.run(args); } }
接下來(lái)我們只需要使用MockMvc
發(fā)送post
請(qǐng)求即可。如果controller
層的post
方法是帶@RequestBody
注解的,可以先將入?yún)?duì)象轉(zhuǎn)換成JSON
字符串。這里使用的是fastjson
。
JSON.toJSONString(param)
經(jīng)過(guò)測(cè)試,如上代碼能正常工作。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
Spring boot實(shí)現(xiàn)文件上傳實(shí)例(多文件上傳)
本篇文章主要介紹了Spring boot實(shí)現(xiàn)文件上傳實(shí)例(多文件上傳),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Java高并發(fā)BlockingQueue重要的實(shí)現(xiàn)類(lèi)詳解
這篇文章主要給大家介紹了關(guān)于Java高并發(fā)BlockingQueue重要的實(shí)現(xiàn)類(lèi)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01淺析Java中關(guān)鍵詞volatile底層的實(shí)現(xiàn)原理
在 Java 并發(fā)編程中,有 3 個(gè)最常用的關(guān)鍵字:synchronized、ReentrantLock 和 volatile,這篇文章主要來(lái)和大家聊聊volatile底層的實(shí)現(xiàn)原理,感興趣的可以了解下2024-02-02優(yōu)化spring?boot應(yīng)用后6s內(nèi)啟動(dòng)內(nèi)存減半
這篇文章主要為大家介紹了優(yōu)化spring?boot后應(yīng)用6s內(nèi)啟動(dòng)內(nèi)存減半的優(yōu)化示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-02-02java字符串切割實(shí)例學(xué)習(xí)(獲取文件名)
在Java中處理一些路徑相關(guān)的問(wèn)題的時(shí)候,如要取出ie瀏覽器上傳文件的文件名,由于ie會(huì)把整個(gè)文件路徑都作為文件名上傳,需要用java.lang.String中的replaceAll或者split來(lái)處理,下面看看使用方法2013-12-12Java工具類(lèi)之@RequestMapping注解
今天帶大家來(lái)學(xué)習(xí)Java工具類(lèi),文中對(duì)注解@RequestMapping作了非常詳細(xì)的介紹,對(duì)正在學(xué)習(xí)java的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05Mybatis-Plus開(kāi)發(fā)提速器mybatis-plus-generator-ui詳解
這篇文章主要介紹了Mybatis-Plus開(kāi)發(fā)提速器mybatis-plus-generator-ui,本文簡(jiǎn)要介紹一款基于Mybatis-Plus的代碼自助生成器,文章通過(guò)實(shí)例集成的方式來(lái)詳細(xì)講解mybatis-plus-generator-ui,從相關(guān)概念到實(shí)際集成案例,以及具體的擴(kuò)展開(kāi)發(fā)介紹,需要的朋友可以參考下2022-11-11IDEA部署Docker鏡像的實(shí)現(xiàn)示例
本文主要介紹了IDEA部署Docker鏡像的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04