LoslegenKostenlos loslegen

Digits with MethodSource

Like most old and popular programming languages, Java has a vast ecosystem, and there are many ways to do one thing.

Consider the LastDigit exercise from the previous lesson. Practice the lesson's syntax by rewriting it using @MethodSource.

Diese Übung ist Teil des Kurses

Introduction to Testing in Java

Kurs anzeigen

Anleitung zur Übung

  • Use the correct annotation for a parameterized test that takes its arguments from a method.
  • Point the annotation to the arguments provider method.
  • Use the same syntax three times to create argument pairs.

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

public class LastDigitWithTests {
  public static void main(String[] args) {
    launchTestsAndPrint(LastDigitTest.class);
  }
}

class LastDigitTest {

    @ParameterizedTest
    // Add the annotation to use a method to provide arguments and point it to the arguments method
    @____("____")
    void testLastDigit(int number, int expected) {
        int actual = LastDigit.lastDigit(number);

        assertEquals(expected, actual);
    }

    private static List getArgs() {
        return List.of(
        	// Create three argument objects for the test using the same syntax
            ____.of(2025, 5),
            ____.of(-2025, 5),
            ____.of(2020, 0)
        );
    }
}
Code bearbeiten und ausführen