Annotation เพิ่มเติม

การทดสอบใน Java เบื้องต้น

Maria Milusheva

Senior Software Engineer

พื้นฐาน: การสร้างอ็อบเจกต์ด้วย .of()

พิจารณาโค้ดนี้:

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 (แก้ไขไม่ได้)

การทดสอบใน Java เบื้องต้น

การส่งอ็อบเจกต์ให้ @ParameterizedTest

พิจารณาคลาสต่อไปนี้:

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;
    }
}
การทดสอบใน Java เบื้องต้น

คลาส Arguments

ใช้ Arguments เพื่อส่งหลาย argument ที่มีชนิดใดก็ได้

Arguments — รองรับอ็อบเจกต์จำนวนเท่าใดก็ได้และชนิดใดก็ได้

ตัวอย่างเช่น:

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

นำไปใช้กับ @ParameterizedTest ประเภทถัดไปได้เลย

การทดสอบใน Java เบื้องต้น

@MethodSource

@MethodSource ช่วยให้ส่งอ็อบเจกต์ใดก็ได้เข้าสู่การทดสอบ

โค้ดทดสอบมีลักษณะดังนี้:

@ParameterizedTest
@MethodSource("provideNames")
void testFullName(Person person, String expectedFullName) {
    assertEquals(person.fullName(person), expectedFullName);
}
การทดสอบใน Java เบื้องต้น

เมธอด provider สำหรับ @MethodSource

เมธอดสำหรับ @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> เป็นแบบที่ง่ายที่สุด

1 https://junit.org/junit5/docs/5.2.0/api/org/junit/jupiter/params/provider/MethodSource.html
การทดสอบใน Java เบื้องต้น

เมธอด provider สำหรับ @MethodSource

เมธอดสำหรับ @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"));
}
การทดสอบใน Java เบื้องต้น

ถ้าต้องการมากกว่าแค่ argument?

พิจารณาการทดสอบฐานข้อมูลนี้:

@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); }
การทดสอบใน Java เบื้องต้น

Annotation @BeforeEach

ใช้ annotation @BeforeEach เพื่อสร้างเมธอดที่รันก่อนการทดสอบแต่ละครั้ง:

import org.junit.jupiter.api.BeforeEach;

ก่อนใช้งาน ให้ประกาศอ็อบเจกต์เป็น field ก่อน:

class MessageProcessorTest {

    private InfoStore infoStore;
    private ErrorStore errorStore;
    private MessageProcessor messageProcessor;
การทดสอบใน Java เบื้องต้น

เมธอดสำหรับ @BeforeEach

สร้างอ็อบเจกต์แต่ละตัวในเมธอดแยก:

@BeforeEach
void setUp() {
    this.infoStore = mock(InfoStore.class);
    this.errorStore = mock(errorStore.class);
    this.messageProcessor = new MessageProcessor(infoStore, errorStore);
}
การทดสอบใน Java เบื้องต้น

โค้ดทดสอบที่กระชับขึ้น

คลาสทดสอบจะกลายเป็น:

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

    verify(infoStore).save("[INFO] Process started."); 
    verifyNoInteractions(errorStore); 
}
การทดสอบใน Java เบื้องต้น

@BeforeEach: ขั้นตอนทั้งหมด

ภาพรวมทั้งหมด:

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 เบื้องต้น

มาฝึกกันเถอะ!

การทดสอบใน Java เบื้องต้น

Preparing Video For Download...