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>

項目使用的是 springboot  2.4.0。

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

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

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

項目目錄結構如下

 

 

 

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,name:張三,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,name:張三,desc:路人}"));  // 可以不用寫成轉義後的json格式

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

 

Tags: