Mockito 結(jié)合 Springboot 進(jìn)行應(yīng)用測(cè)試的方法詳解
Spring Boot可以和大部分流行的測(cè)試框架協(xié)同工作:通過Spring JUnit創(chuàng)建單元測(cè)試;生成測(cè)試數(shù)據(jù)初始化數(shù)據(jù)庫用于測(cè)試;Spring Boot可以跟BDD(Behavier Driven Development)工具、Cucumber和Spock協(xié)同工作,對(duì)應(yīng)用程序進(jìn)行測(cè)試。
在web應(yīng)用程序中,我們主要是對(duì)Service層做單元測(cè)試,以前單元測(cè)試都是使用 junit4 ,對(duì)Controller層做集成測(cè)試或者接口測(cè)試,對(duì)Controller層的測(cè)試一般有兩種方法:(1)發(fā)送http請(qǐng)求;(2)模擬http請(qǐng)求對(duì)象。
第一種方法需要配置回歸環(huán)境,通過修改代碼統(tǒng)計(jì)的策略來計(jì)算覆蓋率;第二種方法是比較正規(guī)的思路。
Mockito網(wǎng)上相關(guān)的文檔不是很多,基本都是入門性質(zhì)的沒有更深層次的使用案例,而且Mockito本身功能也在不斷的完善,導(dǎo)致寫起來比較費(fèi)勁,好多地方完全靠猜。摸索之下算是完成了,把踩過的坑記錄一下,萬一有人需要呢。
下面我將演示如何用Mock對(duì)象測(cè)試Service、Controller層的代碼。
引入相關(guān)jar
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
項(xiàng)目使用的是 springboot2.4.0。
spring-boot-starter-test 中包含 junit5 和Mockito 相關(guān)jar。無需額外引入。
如果想使用 junit4,可以將springboot版本降低,junit4 與 junit5 在一些注解和方法上有區(qū)別,比如注解的引入目錄不同,一些方法進(jìn)行了優(yōu)化,有興趣可以查閱相關(guān)資料,這里就不再贅述。
下面代碼是 junit5 使用樣式。
項(xiàng)目目錄結(jié)構(gòu)如下
Controller類
@RestController @RequestMapping("/api/v1") public class UserController { @Autowired UserService userService; @GetMapping("user/{userId}") public User say(@PathVariable("userId") Long id) { return userService.getUser(id); } @PostMapping("user/edit") public User edit(@RequestBody User user) { return userService.edit(user); } }
Service 實(shí)現(xiàn)類
@Service public class UserServiceImpl implements UserService { @Autowired UserDao userDao; @Override public User getUser(Long id) { return userDao.getUser(id); } @Override public User edit(User user) { return userDao.edit(user); } }
Dao 接口
public interface UserDao { User getUser(Long id); User edit(User user); }
User 類
public class User { private Long id; private String name; private String desc; get()... set()... toString()... }
UserDao 是一個(gè)接口,沒有任何的相關(guān)實(shí)現(xiàn)。所以對(duì)該接口進(jìn)行mock。測(cè)試代碼如下
package com.mmling.mockitodemo;import com.mmling.mockitodemo.controller.UserController;import com.mmling.mockitodemo.dao.UserDao;import com.mmling.mockitodemo.entity.User;import com.mmling.mockitodemo.service.UserService;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;import org.junit.jupiter.api.extension.ExtendWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.boot.test.mock.mockito.MockBean;import org.springframework.http.MediaType;import org.springframework.test.context.junit.jupiter.SpringExtension;import org.springframework.test.web.servlet.MockMvc;import org.springframework.test.web.servlet.ResultActions;import org.springframework.test.web.servlet.setup.MockMvcBuilders;import static org.mockito.ArgumentMatchers.any;import static org.mockito.ArgumentMatchers.anyLong;import static org.mockito.Mockito.times;import static org.mockito.Mockito.verify;import static org.mockito.Mockito.when;import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;/** * @author Robert * @date 2020-11-27 14:38 */@ExtendWith(SpringExtension.class)@SpringBootTest(classes = MockitoDemoApplication.class)public class UserBeanTest { @Autowired UserController controller; @Autowired UserService userService; @MockBean //需要mock的bean,會(huì)自動(dòng)注入到調(diào)用的對(duì)象中 private UserDao userDao; MockMvc mockMvc; /** * 測(cè)試 service 層 */ @Test public void test() { // 定義未實(shí)現(xiàn)的 service 返回 when(userDao.getUser(anyLong())).thenReturn(new User(anyLong(), "張三", "路人")); System.out.println(userService.getUser(12L).toString()); verify(userDao, times(1)).getUser(anyLong()); } /** * 測(cè)試 controller 時(shí),需要構(gòu)建 mvc 環(huán)境 */ @BeforeEach public void setup() { //構(gòu)建mvc環(huán)境 mockMvc = MockMvcBuilders.standaloneSetup(controller).build(); } /** * .perform() : 執(zhí)行一個(gè)MockMvcRequestBuilders的請(qǐng)求;MockMvcRequestBuilders有.get()、.post()、.put()、.delete()等請(qǐng)求。 * .andDo() : 添加一個(gè)MockMvcResultHandlers結(jié)果處理器,可以用于打印結(jié)果輸出(MockMvcResultHandlers.print())。 * .andExpect : 添加MockMvcResultMatchers驗(yàn)證規(guī)則,驗(yàn)證執(zhí)行結(jié)果是否正確。 */ @Test public void testGetUser() throws Exception { // 定義未實(shí)現(xiàn)的 service 返回 when(userDao.getUser(anyLong())).thenReturn(new User(12L, "張三", "路人")); //模擬接口調(diào)用 ResultActions perform = this.mockMvc.perform(get("/api/v1/user/12")); //對(duì)接口響應(yīng)進(jìn)行驗(yàn)證 perform.andExpect(status().isOk()) .andExpect(content().json("{id:12,name:張三,desc:路人}")); // 可以不用寫成轉(zhuǎn)義后的json格式 System.out.println(perform.andReturn().getResponse().getContentAsString()); } @Test public void testEditUser() throws Exception { // 定義未實(shí)現(xiàn)的 service 返回 when(userDao.edit(any(User.class))).thenReturn(new User(12L, "張三", "路人")); //模擬接口調(diào)用 ResultActions perform = this.mockMvc.perform(post("/api/v1/user/edit") .contentType(MediaType.APPLICATION_JSON) .content("{\"id\":12,\"name\":\"張三\",\"desc\":\"路人\"}")); // 必須寫成轉(zhuǎn)義后的json格式,否則沒法轉(zhuǎn)換 //對(duì)接口響應(yīng)進(jìn)行驗(yàn)證 perform.andExpect(status().isOk()) .andExpect(content().json("{id:12,name:張三,desc:路人}")); // 可以不用寫成轉(zhuǎn)義后的json格式 System.out.println(perform.andReturn().getResponse().getContentAsString()); }}
注意:
1.由于這是Spring Boot的測(cè)試,因此我們可通過@Autowired注解織入任何由Spring管理的對(duì)象,或者是通過@Value設(shè)置指定的環(huán)境變量的值。
2.每個(gè)測(cè)試用例用@Test注解修飾。
3.第一個(gè)測(cè)試用中展示了如何測(cè)試 Service 層代碼
4.第二個(gè)第三個(gè)測(cè)試用例中展示了如何通過MockMvc對(duì)象實(shí)現(xiàn)對(duì)RESTful URL接口訂單查詢的測(cè)試。Spring測(cè)試框架提供MockMvc對(duì)象,可以在不需要客戶端-服務(wù)端請(qǐng)求的情況下進(jìn)行MVC測(cè)試,完全在服務(wù)端這邊就可以執(zhí)行Controller的請(qǐng)求,跟啟動(dòng)了測(cè)試服務(wù)器一樣。
5.測(cè)試開始之前需要建立測(cè)試環(huán)境,setup方法被@Before修飾。通過MockMvcBuilders工具,使用 controller 對(duì)象作為參數(shù),創(chuàng)建一個(gè)MockMvc對(duì)象。
6.mockMvc 可以鏈?zhǔn)秸{(diào)用,進(jìn)行接口調(diào)用,并判斷狀態(tài)
//模擬接口調(diào)用 ResultActions perform = this.mockMvc.perform(get("/api/v1/user/12")) .andExpect(status().isOk()) .andExpect(content().json("{id:12,name:張三,desc:路人}")); // 可以不用寫成轉(zhuǎn)義后的json格式
7. content().json() 會(huì)對(duì)結(jié)果進(jìn)行處理,所以判斷的無需轉(zhuǎn)義,但this.mockMvc.perform(post("/api/v1/user/edit").contentType(MediaType.APPLICATION_JSON).content() 中的json是需要手動(dòng)轉(zhuǎn)義的。
到此這篇關(guān)于Mockito 結(jié)合 Springboot 進(jìn)行應(yīng)用測(cè)試的方法詳解的文章就介紹到這了,更多相關(guān)Mockito 結(jié)合 Springboot應(yīng)用測(cè)試內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解如何在低版本的Spring中快速實(shí)現(xiàn)類似自動(dòng)配置的功能
這篇文章主要介紹了詳解如何在低版本的Spring中快速實(shí)現(xiàn)類似自動(dòng)配置的功能,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05Springboot-Starter造輪子之自動(dòng)鎖組件lock-starter實(shí)現(xiàn)
這篇文章主要為大家介紹了Springboot-Starter造輪子之自動(dòng)鎖組件lock-starter實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05SpringBoot使用RedisTemplate.delete刪除指定key失敗的解決辦法
本文主要介紹了SpringBoot使用RedisTemplate.delete刪除指定key失敗的解決辦法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03Java實(shí)現(xiàn)將文件或者文件夾壓縮成zip的詳細(xì)代碼
這篇文章主要介紹了Java實(shí)現(xiàn)將文件或者文件夾壓縮成zip的詳細(xì)代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-11-11單點(diǎn)登錄的概念及SpringBoot實(shí)現(xiàn)單點(diǎn)登錄的操作方法
在本文中,我們將使用Spring Boot構(gòu)建一個(gè)基本的單點(diǎn)登錄系統(tǒng),我們將介紹如何使用Spring Security和JSON Web Tokens(JWTs)來實(shí)現(xiàn)單點(diǎn)登錄功能,本文假設(shè)您已經(jīng)熟悉Spring Boot和Spring Security,感興趣的朋友一起看看吧2024-10-10使用webservice自定義注解處理參數(shù)加解密問題
這篇文章主要介紹了使用webservice自定義注解處理參數(shù)加解密問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12