LoslegenKostenlos loslegen

CsvSource: Digits

You've revisited the lastDigit() from Chapter 1, and this time you've prepared for negative values using Math.abs(). Now that you know you can condense tests using @ParameterizedTest, you've attempted to test all outcomes of lastDigit() in one go.

Verify the method returns the correct digit for positive, negative, and zero-ending integers.

Diese Übung ist Teil des Kurses

Introduction to Testing in Java

Kurs anzeigen

Anleitung zur Übung

  • Use the annotation for inputting multiple values into a test method.
  • Add the method arguments for the test method.

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 LastDigit {
    public static int lastDigit(int a) {
        return Math.abs(a) % 10;
    }
}

class LastDigitTest {

    @ParameterizedTest
    // Use the correct annotation for a pair of integer inputs
    @____(value = {"2025, 5", "-2025, 5", "2020, 0"}) 
    // Write the corresponding argument types for the test method
    void testLastDigit(____ number, ____ expected) {
        int actual = LastDigit.lastDigit(number);

        assertEquals(expected, actual);
    }
}
Code bearbeiten und ausführen