การทดสอบใน Java เบื้องต้น
Maria Milusheva
Senior Software Engineer
พิจารณาโค้ดนี้:
List<Integer> newList = new ArrayList<Integer>();
newList.add(10);
newList.add(20);
newList.add(30);
วิธีที่สั้นและกระชับกว่า:
List<Integer> newList = List.of(10, 20, 30);
ใช้ได้เช่นกันกับ Set และ Map จาก Java Collections
อ็อบเจกต์ที่สร้างด้วย .of() มักจะเป็น immutable (แก้ไขไม่ได้)
พิจารณาคลาสต่อไปนี้:
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;
}
}
ใช้ Arguments เพื่อส่งหลาย argument ที่มีชนิดใดก็ได้
Arguments — รองรับอ็อบเจกต์จำนวนเท่าใดก็ได้และชนิดใดก็ได้
ตัวอย่างเช่น:
Arguments.of(new Person("Monty", "Python"), "Monty Python");
นำไปใช้กับ @ParameterizedTest ประเภทถัดไปได้เลย
@MethodSource ช่วยให้ส่งอ็อบเจกต์ใดก็ได้เข้าสู่การทดสอบ
โค้ดทดสอบมีลักษณะดังนี้:
@ParameterizedTest
@MethodSource("provideNames")
void testFullName(Person person, String expectedFullName) {
assertEquals(person.fullName(person), expectedFullName);
}
เมธอดสำหรับ @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;
}
หมายเหตุ: เมธอดต้องเป็น static
หมายเหตุ: รองรับ return type หลายแบบ; List<Arguments> เป็นแบบที่ง่ายที่สุด
เมธอดสำหรับ @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"));
}
พิจารณาการทดสอบฐานข้อมูลนี้:
@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); }
ใช้ annotation @BeforeEach เพื่อสร้างเมธอดที่รันก่อนการทดสอบแต่ละครั้ง:
import org.junit.jupiter.api.BeforeEach;
ก่อนใช้งาน ให้ประกาศอ็อบเจกต์เป็น field ก่อน:
class MessageProcessorTest {
private InfoStore infoStore;
private ErrorStore errorStore;
private MessageProcessor messageProcessor;
สร้างอ็อบเจกต์แต่ละตัวในเมธอดแยก:
@BeforeEach
void setUp() {
this.infoStore = mock(InfoStore.class);
this.errorStore = mock(errorStore.class);
this.messageProcessor = new MessageProcessor(infoStore, errorStore);
}
คลาสทดสอบจะกลายเป็น:
@Test
void process_savesToInfoStore_whenInfoMessage() {
messageProcessor.process("[INFO] Process started.");
verify(infoStore).save("[INFO] Process started.");
verifyNoInteractions(errorStore);
}
ภาพรวมทั้งหมด:
class MessageProcessorTest { private InfoStore infoStore; // Declare fields@BeforeEach void setUp() { this.infoStore = mock(InfoStore.class); // Initialize fields }@Test void process_savesToInfoStore_whenInfoMessage() { // Use fields } }
การทดสอบใน Java เบื้องต้น