开始使用免费开始使用

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, ____)); 
    }
}
编辑并运行代码