Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Introduction to Testing in Java

Cursus bekijken

Oefeninstructies

  • 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.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

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 bewerken en uitvoeren