Spring Boot學習筆記(五)—— 使用JUnit5編寫單元測試

1.開發環境

  • Mac OS 10.14
  • JDK8
  • Maven 3.5.3
  • Eclipse 4.9.0
  • Spring Boot 2.0.5.RELEASE
  • JUnit 5.1.1

2.JUnit5簡介[1]

JUnit 5跟以前的JUnit版本不一樣,它由幾大不同的模塊組成,這些模塊分別來自三個不同的子項目。
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
主要註解:

  • @BeforeAll 類似於JUnit 4的@BeforeAll,表示使用了該註解的方法應該在當前類中所有使用了@Test@RepeatedTest@ParameterizedTest或者@TestFactory註解的方法之前執行,必須爲static
  • @BeforeEach 類似於JUnit 4的@Before,表示使用了該註解的方法應該在當前類中每一個使用了@Test@RepeatedTest@ParameterizedTest或者@TestFactory註解的方法之前執行
  • @Test 表示該方法是一個測試方法
  • @DisplayName 爲測試類或測試方法聲明一個自定義的顯示名稱
  • @AfterEach 類似於JUnit 4的@After,表示使用了該註解的方法應該在當前類中每一個使用了@Test@RepeatedTest@ParameterizedTest或者@TestFactory註解的方法之後執行
  • @AfterAll 類似於JUnit 4的@AfterClass,表示使用了該註解的方法應該在當前類中所有使用了@Test@RepeatedTest@ParameterizedTest或者@TestFactory註解的方法之後執行,必須爲static
  • @Disable 用於禁用一個測試類或測試方法,類似於JUnit 4的@Ignore
  • @ExtendWith 用於註冊自定義擴展

3.編寫單元測試

1)添加依賴

spring-boot-starter-test默認使用junit4,需要手動添加junit5依賴

		<!-- Junit 5 -->
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-api</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-params</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-engine</artifactId>
			<scope>test</scope>
		</dependency>

2)代碼編寫

JUnit Jupiter附帶了很多JUnit 4就已經存在的斷言方法,並增加了一些適合與Java8 Lambda一起使用的斷言。所有的JUnit Jupiter斷言都是 org.junit.jupiter.api.Assertions 類中static方法。

package cn.swift.mapper;

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
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.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional;

import cn.swift.model.User;

@ExtendWith(SpringExtension.class) //導入spring測試框架[2]
@SpringBootTest  //提供spring依賴注入
@Transactional  //事務管理,默認回滾,如果配置了多數據源記得指定事務管理器
@DisplayName("Test UserMapper")
public class UserMapperTest {

    @Autowired
    private UserMapper userMapper;
    
    private static User user;
    
    @BeforeAll //在所有測試方法前執行,只執行一次
    static void setUp() {
	user = new User();
	user.setUsername("TEST_USER");
	user.setPassword("123456");
	user.setEmail("[email protected]");
	user.setDescription("this is a user for test.");
    }
    
    @Test
    @DisplayName("Add user")
    void addUserTest() {
    //支持java8 lambda
	assertAll("Insert User Success.",
		() -> assertNotNull(userMapper.insertSelective(user)),
		() -> assertNotNull(user.getId()));
    }
    
    @Test
    @DisplayName("Delete user")
    void deleteUserTest() {
	assertEquals(0, userMapper.deleteByPrimaryKey(-1L));
    }
    
    @Test
    @DisplayName("Modify user")
    void modifyUserTest() {
	user.setId(-1L);
	assertEquals(0, userMapper.updateByPrimaryKeySelective(user));
    }
    
    @Test
    @DisplayName("Query user")
    void queryUserTest() {
	assertNull(userMapper.selectByPrimaryKey(-1L));
    }
}

執行結果
UserMapperTest

4.總結

上述代碼只是演示了spring boot + junit最簡單的使用,實際應用時可以做很多測試
如:

  • MockMvc 測試http請求
  • Mockito 模擬測試對象

強大的功能需要根據實際需求來選擇使用。
演示代碼地址:https://github.com/tsfans/spring-boot-scaffold

5.參考文獻

[1] JUnit 5官方文檔 https://sjyuan.cc/junit5/user-guide-cn/#3-編寫測試
[2] Spring Boot 2官方文檔 https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/#boot-features-testing

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章