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.
This exercise is part of the course
Introduction to Testing in Java
Exercise instructions
- 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 failing.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
import org.junit.jupiter.api.Test;
import static com.datacamp.util.testing.CustomJUnitTestLauncher.launchTestsAndPrint;
import static org.junit.jupiter.api.Assertions.*;
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
____;
}
}