Hướng Dẫn Thực Hành Kiểm Thử Đơn Vị Với JUnit 5

Cấu Hình Phụ Thuộc Maven

Để bắt đầu sử dụng JUnit 5 trong dự án Java, cần khai báo thư viện gốc junit-jupiter trong file pom.xml. Phiên bản ổn định thường được khuyến nghị là 5.8.1 hoặc mới hơn.

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.8.1</version>
    <scope>test</scope>
</dependency>

Các Annotation Cơ Bản

JUnit 5 sử dụng các annotation để đánh dấu phương thức kiểm tra. Hai annotation quan trọng nhất là @Test để định nghĩa một case kiểm tra và @DisplayName để đặt tên hiển thị cho case đó.

Lưu ý quan trọng khi tổ chức code:

  • Tên class kiểm tra nên bắt đầu bằng hậu tố "Test", ví dụ: UserServiceTest.
  • Các phương thức kiểm tra cần được khai báo là public.
public class UserServiceTest { 
    @Test 
    @DisplayName("Kiểm tra chức năng đăng nhập cơ bản") 
    public void validateLoginFunction() {  
        System.out.println("Đang thực thi kiểm tra đăng nhập");  
    }
}

Để thực thi kiểm tra, sử dụng các lệnh Maven sau:

  • mvn test: Chạy toàn bộ các case trong dự án.
  • mvn test -Dtest=UserServiceTest: Chỉ chạy class kiểm tra cụ thể.

Quản Lý Vòng Đời Test

Trong quá trình kiểm thử tự động, việc thiết lập môi trường (setup) và dọn dẹp dữ liệu (teardown) là rất cần thiết để đảm bảo tính độc lập giữa các lần chạy.

JUnit 5 cung cấp 4 annotation chính cho vòng đời:

  • @BeforeAll: Chạy một lần duy nhất trước tất cả các phương thức test trong class (yêu cầu method static).
  • @AfterAll: Chạy một lần duy nhất sau khi tất cả các phương thức test hoàn thành (yêu cầu method static).
  • @BeforeEach: Chạy trước mỗi phương thức test.
  • @AfterEach: Chạy sau mỗi phương thức test.
public class LifecycleDemo { 
    @BeforeAll  
    public static void initGlobalEnvironment() {  
        System.out.println("Khởi tạo môi trường chung");  
    }  
    @AfterAll  
    public static void cleanupGlobalEnvironment() {  
        System.out.println("Dọn dẹp môi trường chung");  
    }  
    @BeforeEach  
    public void prepareTestData() {  
        System.out.println("Chuẩn bị dữ liệu cho từng case");  
    }  
    @AfterEach  
    public void resetTestData() {  
        System.out.println("Reset dữ liệu sau mỗi case");  
    }
} 

Phân Nhóm Và Thứ Tự Thực Thi

Để quản lý các case kiểm tra phức tạp, bạn có thể gán nhãn (tag) hoặc thiết lập thứ tự ưu tiên chạy.

  • @Tag: Đánh dấu nhóm kiểm tra, hỗ trợ lọc khi chạy.
  • @Order: Quy định số thứ tự thực thi các method trong class.
public class OrderedTest { 
    @Test  
    @Tag("integration")
    @Order(1)
    public void stepOne() {  
        System.out.println("Bước xử lý đầu tiên");  
    }

    @Test  
    @Order(2)
    public void stepTwo() {  
        System.out.println("Bước xử lý tiếp theo");  
    }
}

Để chạy chỉ các method thuộc nhóm tag cụ thể:

mvn test -Dgroups="integration"

Sử Dụng Phép Khẳng Định (Assertions)

Assertion dùng để so sánh kết quả thực tế với giá trị kỳ vọng. Một số method phổ biến trong JUnit 5:

  • assertEquals(expected, actual): Kiểm tra sự bằng nhau.
  • assertTrue(condition): Kiểm tra điều kiện đúng.
  • assertNull(object) / assertNotNull(object): Kiểm tra giá trị null.
  • assertThrows(exception, executable): Kiểm tra ngoại lệ được ném ra.
  • assertTimeout(duration, executable): Kiểm tra thời gian thực thi.

Dưới đây là ví dụ kết hợp kiểm tra API sử dụng OkHttp và xử lý JSON với FastJSON. Cần thêm phụ thuộc sau:

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>4.9.0</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.83</version>
</dependency>

Triển khai test case kiểm tra phản hồi API:

public class ApiValidationTest {
    @Test
    public void verifyHttpGetResponse() throws IOException {
        OkHttpClient httpClient = new OkHttpClient.Builder().build();
        Request httpRequest = new Request.Builder()
                .url("https://httpbin.org/get?x=10&y=20")
                .addHeader("Authorization", "Bearer token123")
                .build();
        Response httpResponse = httpClient.newCall(httpRequest).execute();

        assertTrue(httpResponse.isSuccessful()); 
        assertEquals(200, httpResponse.code());  
        assertNotNull(httpResponse.body()); 

        String content = httpResponse.body().string();
        JSONObject jsonResult = JSON.parseObject(content);
        JSONObject params = jsonResult.getJSONObject("args");
        assertEquals("10", params.getString("x"));
        assertEquals("20", params.getString("y"));
    }
}

Kiểm Thử Với Dữ Liệu Tham Số Hóa

Thay vì viết nhiều method test giống nhau, @ParameterizedTest cho phép chạy một method với nhiều bộ dữ liệu đầu vào khác nhau.

  • @ValueSource: Truyền mảng giá trị đơn lẻ.
  • @CsvSource: Truyền dữ liệu dạng CSV ngay trong code.
  • @CsvFileSource: Đọc dữ liệu từ file CSV bên ngoài.
  • @NullSource, @EmptySource: Kiểm tra giá trị null hoặc rỗng.

Ví dụ sử dụng @ValueSource với kiểu số nguyên:

public class ParameterizedDemo { 
    @ParameterizedTest  
    @ValueSource(ints = { 10, 20, 30 })  
    public void verifyNumberInput(int value) {  
        System.out.println("Giá trị kiểm tra: " + value);  
    } 
}

Ví dụ sử dụng @CsvSource để kiểm tra phép toán:

public class MathTest { 
    @ParameterizedTest  
    @CsvSource({"1,2,3", "4,5,9", "10,10,20"})  
    public void testAddition(int a, int b, int expected) {  
        int result = a + b;
        assertEquals(expected, result);  
    }
}

Để sử dụng file CSV, tạo file cases.csv trong thư mục src/test/resources:

a,b,expected
1,2,3
4,5,9

Code kiểm tra tương ứng:

public class FileSourceTest { 
    @ParameterizedTest  
    @CsvFileSource(resources = "/cases.csv", numLinesToSkip = 1)  
    public void testWithDataFile(int a, int b, int expected) {  
        assertEquals(expected, a + b);  
    } 
} 

Xuất Báo Cáo Kết Quả Dạng HTML

Sử dụng Maven Surefire

Cấu hình plugin maven-surefire-plugin trong phần <build> của pom.xml:

<plugin>  
    <artifactId>maven-surefire-plugin</artifactId>  
    <version>2.22.2</version>  
</plugin>

Chạy lệnh sau để tạo báo cáo:

mvn surefire-report:report

Kết quả sẽ nằm trong thư mục target/reports/.

Sử dụng Allure Report

Allure cung cấp giao diện báo cáo đẹp và chi tiết hơn. Cần thêm phụ thuộc:

<dependency>
    <groupId>io.qameta.allure</groupId>
    <artifactId>allure-junit5</artifactId>
    <version>2.13.2</version>
</dependency>

Cấu hình plugin Allure trong Maven:

<plugin>
    <groupId>io.qameta.allure</groupId>
    <artifactId>allure-maven</artifactId>
    <version>2.10.0</version>
    <configuration>
        <reportVersion>2.13.2</reportVersion>
        <allureDownloadUrl>https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline/2.13.2/allure-commandline-2.13.2.zip</allureDownloadUrl>
    </configuration>
</plugin>

Sau khi chạy mvn test, dữ liệu báo cáo sẽ được sinh ra. Sử dụng công cụ dòng lệnh để xem báo cáo:

allure serve

Lệnh này sẽ khởi động server cục bộ và mở báo cáo trên trình duyệt. Đảm bảo công cụ allure-commandline đã được cài đặt và cấu hình biến môi trường PATH.

Thẻ: junit5 java-unit-testing maven-surefire allure-report okhttp

Đăng vào ngày 18 tháng 6 lúc 02:36