ParameterizedTest:清單加總
在這個練習中,你會練習在參數化測試中提供物件作為引數。請思考 sumList 方法以及它的測試案例。
完成參數化測試與引數提供的方法。
本練習屬於課程
Java 測試入門
練習說明
- 使用正確的 method source 註解,並提供方法名稱。
- 宣告引數提供的方法,並確保它有正確的關鍵字與回傳型別。
- 對於最後一個
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, ____));
}
}