1. 程式人生 > 程式設計 >Mockito 結合 Springboot 進行應用測試的方法詳解

Mockito 結合 Springboot 進行應用測試的方法詳解

Spring Boot可以和大部分流行的測試框架協同工作:通過Spring JUnit建立單元測試;生成測試資料初始化資料庫用於測試;Spring Boot可以跟BDD(Behavier Driven Development)工具、Cucumber和Spock協同工作,對應用程式進行測試。

在web應用程式中,我們主要是對Service層做單元測試,以前單元測試都是使用 junit4 ,對Controller層做整合測試或者介面測試,對Controller層的測試一般有兩種方法:(1)傳送http請求;(2)模擬http請求物件。

第一種方法需要配置迴歸環境,通過修改程式碼統計的策略來計算覆蓋率;第二種方法是比較正規的思路。

Mockito網上相關的文件不是很多,基本都是入門性質的沒有更深層次的使用案例,而且Mockito本身功能也在不斷的完善,導致寫起來比較費勁,好多地方完全靠猜。摸索之下算是完成了,把踩過的坑記錄一下,萬一有人需要呢。

下面我將演示如何用Mock物件測試Service、Controller層的程式碼。

引入相關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>

專案使用的是 springboot2.4.0。

spring-boot-starter-test 中包含 junit5 和Mockito 相關jar。無需額外引入。

如果想使用 junit4,可以將springboot版本降低,junit4 與 junit5 在一些註解和方法上有區別,比如註解的引入目錄不同,一些方法進行了優化,有興趣可以查閱相關資料,這裡就不再贅述。

下面程式碼是 junit5 使用樣式。

專案目錄結構如下

Mockito 結合 Springboot 進行應用測試的方法詳解

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 實現類

@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 是一個介面,沒有任何的相關實現。所以對該介面進行mock。測試程式碼如下

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,會自動注入到呼叫的物件中  private UserDao userDao;  MockMvc mockMvc;  /**   * 測試 service 層   */  @Test  public void test() {    // 定義未實現的 service 返回    when(userDao.getUser(anyLong())).thenReturn(new User(anyLong(),"張三","路人"));    System.out.println(userService.getUser(12L).toString());    verify(userDao,times(1)).getUser(anyLong());  }  /**   * 測試 controller 時,需要構建 mvc 環境   */  @BeforeEach  public void setup() {    //構建mvc環境    mockMvc = MockMvcBuilders.standaloneSetup(controller).build();  }  /**   * .perform() : 執行一個MockMvcRequestBuilders的請求;MockMvcRequestBuilders有.get()、.post()、.put()、.delete()等請求。   * .andDo() : 新增一個MockMvcResultHandlers結果處理器,可以用於列印結果輸出(MockMvcResultHandlers.print())。   * .andExpect : 新增MockMvcResultMatchers驗證規則,驗證執行結果是否正確。   */  @Test  public void testGetUser() throws Exception {    // 定義未實現的 service 返回    when(userDao.getUser(anyLong())).thenReturn(new User(12L,"路人"));    //模擬介面呼叫    ResultActions perform = this.mockMvc.perform(get("/api/v1/user/12"));    //對介面響應進行驗證    perform.andExpect(status().isOk())        .andExpect(content().json("{id:12,name:張三,desc:路人}")); // 可以不用寫成轉義後的json格式    System.out.println(perform.andReturn().getResponse().getContentAsString());  }  @Test  public void testEditUser() throws Exception {    // 定義未實現的 service 返回    when(userDao.edit(any(User.class))).thenReturn(new User(12L,"路人"));    //模擬介面呼叫    ResultActions perform = this.mockMvc.perform(post("/api/v1/user/edit")        .contentType(MediaType.APPLICATION_JSON)        .content("{\"id\":12,\"name\":\"張三\",\"desc\":\"路人\"}"));  // 必須寫成轉義後的json格式,否則沒法轉換    //對介面響應進行驗證    perform.andExpect(status().isOk())        .andExpect(content().json("{id:12,desc:路人}")); // 可以不用寫成轉義後的json格式    System.out.println(perform.andReturn().getResponse().getContentAsString());  }}

 注意:

  1.由於這是Spring Boot的測試,因此我們可通過@Autowired註解織入任何由Spring管理的物件,或者是通過@Value設定指定的環境變數的值。

  2.每個測試用例用@Test註解修飾。

  3.第一個測試用中展示瞭如何測試 Service 層程式碼

  4.第二個第三個測試用例中展示瞭如何通過MockMvc物件實現對RESTful URL介面訂單查詢的測試。Spring測試框架提供MockMvc物件,可以在不需要客戶端-服務端請求的情況下進行MVC測試,完全在服務端這邊就可以執行Controller的請求,跟啟動了測試伺服器一樣。

  5.測試開始之前需要建立測試環境,setup方法被@Before修飾。通過MockMvcBuilders工具,使用 controller 物件作為引數,建立一個MockMvc物件。

  6.mockMvc 可以鏈式呼叫,進行介面呼叫,並判斷狀態

//模擬介面呼叫
ResultActions perform = this.mockMvc.perform(get("/api/v1/user/12"))
        .andExpect(status().isOk())
        .andExpect(content().json("{id:12,desc:路人}")); // 可以不用寫成轉義後的json格式

  7. content().json() 會對結果進行處理,所以判斷的無需轉義,但this.mockMvc.perform(post("/api/v1/user/edit").contentType(MediaType.APPLICATION_JSON).content() 中的json是需要手動轉義的。

到此這篇關於Mockito 結合 Springboot 進行應用測試的方法詳解的文章就介紹到這了,更多相關Mockito 結合 Springboot應用測試內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!