src/
└── test/
└── java/
└── com/
└── example/
├── exceptions/
│ └── CustomException.java
├── utils/
│ └── ExceptionUtils.java
├── pages/
│ └── SamplePage.java
└── tests/
└── ExceptionTests.java
Testing multiple exceptions in JUnit - Framework Patterns
- Test Classes: Located in
tests/, contain JUnit test methods that verify multiple exceptions usingassertThrowsorassertAll. - Page Objects / Business Logic: In
pages/orutils/, contain methods that may throw different exceptions. - Custom Exceptions: Defined in
exceptions/folder for specific error cases. - Utilities: Helper methods for exception handling or test data setup.
- Configuration: Externalized configs for environment or test parameters (not shown here).
- Use
src/test/resourcesfor environment-specific properties (e.g., test data, exception messages). - Use Maven or Gradle profiles to switch environments if needed.
- Keep exception messages or codes configurable to verify in tests.
- JUnit 5 annotations and extensions configured in
pom.xmlorbuild.gradle.
- JUnit 5 generates XML reports consumable by CI tools like Jenkins or GitHub Actions.
- Use Surefire or Gradle test task to run tests and produce reports.
- Reports show which exception tests passed or failed with stack traces.
- Integrate with code coverage tools (e.g., JaCoCo) to ensure exception paths are tested.
- Fail build on test failures to maintain quality.
- Use
assertThrowsfor each expected exception separately to keep tests clear. - Use
assertAllto group multiple exception assertions in one test method. - Define custom exceptions to make error cases explicit and testable.
- Keep exception messages consistent and verify them in assertions for clarity.
- Write small focused tests for each exception scenario to simplify debugging.
Where would you add a new test method that verifies a third custom exception in this framework structure?
Practice
Which JUnit method is best to test that a specific exception is thrown by a method?
Solution
Step 1: Understand the purpose of
This method checks if a specific exception is thrown when running a code block.assertThrows()Step 2: Compare with other assertion methods
assertEquals()checks values,assertTrue()checks boolean conditions, andassertNull()checks for null values, none test exceptions.Final Answer:
assertThrows() -> Option AQuick Check:
Test exceptions with assertThrows() [OK]
- Using assertEquals() to test exceptions
- Trying to catch exceptions manually in tests
- Confusing assertTrue() with exception testing
Which of the following is the correct syntax to test that IllegalArgumentException is thrown by myMethod() in JUnit 5?
?
Solution
Step 1: Recall assertThrows() method signature
The correct order is the exception class first, then a lambda with the code to test.Step 2: Check each option's syntax
assertThrows(IllegalArgumentException.class, () -> myMethod()); matches the correct syntax:assertThrows(Exception.class, () -> method());. Others have wrong order or missing lambda.Final Answer:
assertThrows(IllegalArgumentException.class, () -> myMethod()); -> Option CQuick Check:
Exception class first, then lambda [OK]
- Swapping parameters order in assertThrows
- Not using a lambda expression
- Passing method call instead of lambda
Given the following JUnit 5 test code, what will be the test result?
void testMultipleExceptions() {
assertThrows(NullPointerException.class, () -> { throw new NullPointerException(); });
assertThrows(IllegalStateException.class, () -> { throw new IllegalArgumentException(); });
}Solution
Step 1: Analyze first assertThrows
It expects NullPointerException and throws NullPointerException, so it passes.Step 2: Analyze second assertThrows
It expects IllegalStateException but throws IllegalArgumentException, so it fails.Final Answer:
Test fails because second assertThrows expects IllegalStateException but gets IllegalArgumentException -> Option AQuick Check:
Expected exception must match thrown exception [OK]
- Assuming any exception passes assertThrows
- Ignoring mismatch in exception types
- Thinking assertThrows catches all exceptions
Identify the error in the following JUnit 5 test code that tries to test two exceptions:
@Test
void testExceptions() {
assertThrows(NullPointerException.class, () -> method1());
assertThrows(IllegalArgumentException.class, () -> method2());
assertThrows(IllegalStateException.class, () -> method3());
}Assuming method2() throws IllegalStateException instead of IllegalArgumentException.
Solution
Step 1: Check expected vs actual exceptions for method2()
Expected is IllegalArgumentException but actual thrown is IllegalStateException, so assertThrows fails.Step 2: Confirm other assertThrows calls
Calls for method1() and method3() are correct or irrelevant to this error.Final Answer:
The second assertThrows will fail because expected and actual exceptions differ -> Option BQuick Check:
Expected exception must match thrown exception [OK]
- Assuming multiple assertThrows calls cause failure
- Ignoring mismatch in exception types
- Thinking assertThrows tests method calls, not exceptions
You want to test that a method process() throws either IOException or TimeoutException depending on conditions. Which is the best way to write JUnit 5 tests for both exceptions?
Solution
Step 1: Understand assertThrows usage
assertThrows tests one exception type per call; it cannot test multiple exceptions at once.Step 2: Best practice for multiple exceptions
Write separate tests for each expected exception to keep tests clear and focused.Final Answer:
Write two separate tests each using assertThrows for one exception -> Option DQuick Check:
One assertThrows per exception per test [OK]
- Trying to test multiple exceptions in one assertThrows
- Catching exceptions manually instead of using assertThrows
- Ignoring one exception in tests
Ready to prove it?
Pass the test and this pattern moves unknown
