Test Overview
This test uses the @NullAndEmptySource annotation in JUnit to run the same test method twice: once with a null input and once with an empty string. It verifies that the method handles both cases correctly by returning false.
This test uses the @NullAndEmptySource annotation in JUnit to run the same test method twice: once with a null input and once with an empty string. It verifies that the method handles both cases correctly by returning false.
import static org.junit.jupiter.api.Assertions.assertFalse; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.NullAndEmptySource; public class InputValidatorTest { @ParameterizedTest @NullAndEmptySource void testIsValidInputWithNullAndEmpty(String input) { InputValidator validator = new InputValidator(); boolean result = validator.isValidInput(input); assertFalse(result, "Input should be invalid for null or empty string"); } } class InputValidator { public boolean isValidInput(String input) { return input != null && !input.isEmpty(); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | JUnit test runner initializes the test class InputValidatorTest | - | PASS |
| 2 | Runs parameterized test with input = null | InputValidator instance created; input parameter is null | assertFalse(result) where result = isValidInput(null) | PASS |
| 3 | Runs parameterized test with input = "" (empty string) | InputValidator instance created; input parameter is empty string | assertFalse(result) where result = isValidInput("") | PASS |
| 4 | Test ends | All parameterized test cases executed | - | PASS |