SpringBoot Controller Post接口單元測(cè)試示例
概述
在日常的開發(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)輔助代碼即可搞定。
依賴的包
<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類mock掉。
@MockBean private UserService userService;
使用Spring Boot Test的時(shí)候,它需要一個(gè)ApplicationContext,我們可以在@SpringBootTest注解中使用classes屬性來指定。
@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);
}
}
接下來我們只需要使用MockMvc發(fā)送post請(qǐng)求即可。如果controller層的post方法是帶@RequestBody注解的,可以先將入?yún)?duì)象轉(zhuǎn)換成JSON字符串。這里使用的是fastjson。
JSON.toJSONString(param)
經(jīng)過測(cè)試,如上代碼能正常工作。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
詳解SpringSecurity中的Authentication信息與登錄流程
這篇文章主要介紹了SpringSecurity中的Authentication信息與登錄流程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
SpringBoot Admin健康檢查功能的實(shí)現(xiàn)
admin主要就是告訴運(yùn)維人員,服務(wù)出現(xiàn)異常,然后進(jìn)行通知(微信、郵件、短信、釘釘?shù)龋┛梢苑浅?焖偻ㄖ竭\(yùn)維人員,相當(dāng)報(bào)警功能,接下來通過本文給大家介紹SpringBoot Admin健康檢查的相關(guān)知識(shí),一起看看吧2021-06-06
利用線程實(shí)現(xiàn)動(dòng)態(tài)顯示系統(tǒng)時(shí)間
編寫Applet小程序,通過在HTML文檔中接收參數(shù),顯示當(dāng)前的系統(tǒng)時(shí)間,需要的朋友可以參考下2015-10-10
RocketMQ特性Broker存儲(chǔ)事務(wù)消息實(shí)現(xiàn)
這篇文章主要為大家介紹了RocketMQ特性Broker存儲(chǔ)事務(wù)消息實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Java模板動(dòng)態(tài)生成word文件的方法步驟
最近項(xiàng)目中需要根據(jù)模板生成word文檔,模板文件也是word文檔。本文使用使用freemarker模板生成word文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
Java單表實(shí)現(xiàn)評(píng)論回復(fù)功能(多種實(shí)現(xiàn)方式)
這篇文章主要介紹了Java單表實(shí)現(xiàn)評(píng)論回復(fù)功能,大家都知道評(píng)論功能有多種實(shí)現(xiàn)方式,本文逐一給大家詳細(xì)講解,需要的朋友可以參考下2023-03-03

