Nhập môn Kiểm thử trong Java
Maria Milusheva
Senior Software Engineer
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 Set và Map trong Java Collections
Đối tượng tạo bằng .of() thường là bất biến (không thể thay đổi)
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;
}
}
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.
@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);
}
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
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"));
}
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); }
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;
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);
}
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);
}
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