Aan de slagGa gratis aan de slag

ParameterizedTest: List sum

In this exercise, you will practice providing objects as arguments for parameterized tests. Consider the sumList method and the test cases for it.

Complete the parameterized test and the arguments method.

Deze oefening maakt deel uit van de cursus

Introduction to Testing in Java

Cursus bekijken

Oefeninstructies

  • Use the correct annotation for a method source and provide the method name.
  • Declare the arguments provider method, making sure it has the correct keywords and return type.
  • For the final Arguments object, use null and its expected sum.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

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, ____)); 
    }
}
Code bewerken en uitvoeren