ParameterizedTest: ผลรวมของ List
ในแบบฝึกหัดนี้ จะได้ฝึกส่งออบเจกต์เป็น argument สำหรับ parameterized test ศึกษาเมธอด sumList และ test case ต่างๆ ที่เกี่ยวข้อง
เติม parameterized test และเมธอดที่ใช้ระบุ argument ให้สมบูรณ์
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การทดสอบใน Java เบื้องต้น
คำแนะนำการฝึกหัด
- ใช้ annotation ที่ถูกต้องสำหรับ method source และระบุชื่อเมธอด
- ประกาศเมธอดที่ใช้ระบุ argument โดยตรวจสอบให้มี keyword และ return type ที่ถูกต้อง
- สำหรับออบเจกต์
Argumentsตัวสุดท้าย ให้ใช้nullพร้อมกับผลรวมที่คาดหวัง
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
public class SumOfList {
public static void main(String[] args) {
launchTestsAndPrint(ListSumTest.class);
}
}
class ListSumTest {
@ParameterizedTest
// Invoke the correct annotation for a method source
@____("____")
void sumList_sumsList(List list, int expectedSum) {
int actualSum = ListSum.sumList(list);
assertEquals(expectedSum, actualSum);
}
// Declare the correct method signature
____ ____ List listsAndSums() {
return List.of(
Arguments.of(List.of(1, 2, 3, 4), 10),
Arguments.of(List.of(-5, 5), 0),
Arguments.of(List.of(), 0),
// Create a null list and the expected sum in that case
Arguments.of(null, ____));
}
}