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.
Cet exercice fait partie du cours
Introduction to Testing in Java
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.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de 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
____;
}
}