Chú thích bổ sung

Nhập môn Kiểm thử trong Java

Maria Milusheva

Senior Software Engineer

Kiến thức nền: Tạo đối tượng với .of()

Xem ví dụ:

List<Integer> newList = new ArrayList<Integer>();
newList.add(10);
newList.add(20);
newList.add(30);

Cách nhanh và ngắn hơn:

List<Integer> newList = List.of(10, 20, 30);
  • Hoạt động tương tự cho SetMap trong Java Collections

  • Đối tượng tạo bằng .of() thường là bất biến (không thể thay đổi)

Nhập môn Kiểm thử trong Java

Truyền đối tượng vào @ParameterizedTest

Xem lớp sau:

class Person {
    String firstName;
    String lastName;
    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
    public String fullName(Person person) {
        return firstName + " " + lastName;
    }
}
Nhập môn Kiểm thử trong Java

Lớp Arguments

Ta dùng Arguments để truyền nhiều đối số kiểu bất kỳ.

Arguments có thể chứa số lượng đối tượng bất kỳ.

Ví dụ:

Arguments.of(new Person("Monty", "Python"), "Monty Python");

Ta có thể dùng nó trong kiểu @ParameterizedTest tiếp theo.

Nhập môn Kiểm thử trong Java

@MethodSource

@MethodSource cho phép truyền bất kỳ đối tượng nào vào test.

Bài test như sau:

@ParameterizedTest
@MethodSource("provideNames")
void testFullName(Person person, String expectedFullName) {
    assertEquals(person.fullName(person), expectedFullName);
}
Nhập môn Kiểm thử trong Java

Phương thức @MethodSource

Phương thức cho @MethodSource:

private static List<Arguments> provideNames() {
    List<Arguments> args = new ArrayList<>();
    args.add(Arguments.of(new Person("Robert", "Martin"), "Robert Martin"));
    args.add(Arguments.of(new Person("Heinz", "Kabutz"), "Heinz Kabutz"));

    return args;
}

Lưu ý: phương thức phải là static

Lưu ý: nhiều kiểu trả về được phép; List<Arguments> là đơn giản nhất

1 https://junit.org/junit5/docs/5.2.0/api/org/junit/jupiter/params/provider/MethodSource.html
Nhập môn Kiểm thử trong Java

Phương thức @MethodSource

Phương thức cho @MethodSource:

private static List<Arguments> provideNames() {
    return List.of(
        Arguments.of(new Person("John", "Doe"), "John Doe"),
        Arguments.of(new Person("Jane", "Doe"), "Jane Doe"),
        Arguments.of(new Person("Alice", "Bob"), "Alice Bob"));
}
Nhập môn Kiểm thử trong Java

Nếu cần nhiều hơn các đối số?

Xem bài kiểm thử cơ sở dữ liệu:

@Test
void process_savesToInfoStore_whenInfoMessage() {
  InfoStore infoStore = mock(InfoStore.class);
  ErrorStore errorStore = mock(ErrorStore.class);
  MessageProcessor messageProcessor = new MessageProcessor(infoStore, errorStore);

messageProcessor.saveMessage("[INFO] Process started.");
verify(infoStore).save("[INFO] Process started."); verifyNoInteractions(errorStore); }
Nhập môn Kiểm thử trong Java

Chú thích @BeforeEach

Ta có thể dùng @BeforeEach để tạo phương thức chạy trước mỗi test:

import org.junit.jupiter.api.BeforeEach;

Để dùng, trước tiên khai báo đối tượng dưới dạng trường:

class MessageProcessorTest {

    private InfoStore infoStore;
    private ErrorStore errorStore;
    private MessageProcessor messageProcessor;
Nhập môn Kiểm thử trong Java

Phương thức cho @BeforeEach

Tạo mỗi đối tượng trong một phương thức riêng:

@BeforeEach
void setUp() {
    this.infoStore = mock(InfoStore.class);
    this.errorStore = mock(errorStore.class);
    this.messageProcessor = new MessageProcessor(infoStore, errorStore);
}
Nhập môn Kiểm thử trong Java

Bài test rút gọn

Lớp kiểm thử sau đó thành:

@Test
void process_savesToInfoStore_whenInfoMessage() {
    messageProcessor.process("[INFO] Process started.");

    verify(infoStore).save("[INFO] Process started."); 
    verifyNoInteractions(errorStore); 
}
Nhập môn Kiểm thử trong Java

Luồng đầy đủ với @BeforeEach

Tổng hợp:

class MessageProcessorTest {
    private InfoStore infoStore; // Khai báo trường


@BeforeEach void setUp() { this.infoStore = mock(InfoStore.class); // Khởi tạo trường }
@Test void process_savesToInfoStore_whenInfoMessage() { // Sử dụng các trường } }
Nhập môn Kiểm thử trong Java

Ayo berlatih!

Nhập môn Kiểm thử trong Java

Preparing Video For Download...