LoslegenKostenlos loslegen

Digits of a number fail

Now that you've written a successful test, write a test that will fail when it encounters unexpected behavior at an edge case.

Software engineers often intentionally write failing tests to demonstrate the existence of a bug.

The necessary JUnit packages have been imported for you.

Diese Übung ist Teil des Kurses

Introduction to Testing in Java

Kurs anzeigen

Anleitung zur Übung

  • Enter the correct annotation that will enable JUnit to execute this test.
  • Call the method under test.
  • Use the correct JUnit assertion to assert expected behavior - the test fails.

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 a % 10;
    }
}

class LastDigitTest {
    // Use the correct annotation to mark this as a JUnit test
    @____
    void testLastDigit() {
        int number = -2025;
        int expected = 5;

        // Call the method under test
        int actual = LastDigit.____(____);

        // Use the correct JUnit assertion for the scenario
        ____(expected, actual);
    }
}
Code bearbeiten und ausführen