Test Overview
This test checks that a method throws the correct exceptions under different error conditions. It verifies that the method throws IllegalArgumentException when input is an empty string, and NullPointerException when input is null.
This test checks that a method throws the correct exceptions under different error conditions. It verifies that the method throws IllegalArgumentException when input is an empty string, and NullPointerException when input is null.
import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Test; public class ExceptionTest { public void processInput(String input) { if (input == null) { throw new NullPointerException("Input cannot be null"); } if (input.isEmpty()) { throw new IllegalArgumentException("Input cannot be empty"); } } @Test public void testProcessInput_throwsIllegalArgumentException() { ExceptionTest et = new ExceptionTest(); assertThrows(IllegalArgumentException.class, () -> et.processInput("") ); } @Test public void testProcessInput_throwsNullPointerException() { ExceptionTest et = new ExceptionTest(); assertThrows(NullPointerException.class, () -> et.processInput(null) ); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts for testProcessInput_throwsIllegalArgumentException | JUnit test runner initialized | - | PASS |
| 2 | Calls processInput with empty string "" | Method executes and checks input | - | PASS |
| 3 | Throws IllegalArgumentException with message "Input cannot be empty" | Exception thrown as expected | assertThrows verifies IllegalArgumentException is thrown | PASS |
| 4 | Test ends for testProcessInput_throwsIllegalArgumentException | Test passed | - | PASS |
| 5 | Test starts for testProcessInput_throwsNullPointerException | JUnit test runner ready for next test | - | PASS |
| 6 | Calls processInput with null | Method executes and checks input | - | PASS |
| 7 | Throws NullPointerException with message "Input cannot be null" | Exception thrown as expected | assertThrows verifies NullPointerException is thrown | PASS |
| 8 | Test ends for testProcessInput_throwsNullPointerException | Test passed | - | PASS |