0
0
JUnittesting~15 mins

Checking exception type hierarchy in JUnit - Build an Automation Script

Choose your learning style9 modes available
Verify that a method throws the correct exception type and its hierarchy
Preconditions (2)
Step 1: Call the method 'processData' with invalid input 'null'
Step 2: Catch the exception thrown
Step 3: Verify that the exception is an instance of IllegalArgumentException
Step 4: Verify that the exception is also an instance of RuntimeException
Step 5: Verify that the exception is not an instance of IOException
✅ Expected Result: The test confirms that the thrown exception is IllegalArgumentException and it correctly inherits from RuntimeException but not from IOException
Automation Requirements - JUnit 5
Assertions Needed:
assertThrows to check exception type
assertTrue with instanceof to check exception hierarchy
assertFalse with instanceof to check negative hierarchy
Best Practices:
Use assertThrows for exception testing
Use clear and descriptive assertion messages
Avoid catching exceptions manually inside test methods
Use meaningful test method names
Automated Solution
JUnit
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

class ExceptionHierarchyTest {

    void processData(String input) {
        if (input == null) {
            throw new IllegalArgumentException("Input cannot be null");
        }
        // processing logic
    }

    @Test
    void testProcessDataThrowsCorrectException() {
        IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> {
            processData(null);
        }, "Expected processData to throw IllegalArgumentException for null input");

        assertTrue(thrown instanceof RuntimeException, "Exception should be instance of RuntimeException");
        assertFalse(thrown instanceof java.io.IOException, "Exception should not be instance of IOException");
    }
}

The processData method throws IllegalArgumentException when given null input.

The test method testProcessDataThrowsCorrectException uses assertThrows to check that the exception thrown is exactly IllegalArgumentException.

Then, assertTrue checks that this exception is also a RuntimeException, confirming the inheritance hierarchy.

Finally, assertFalse confirms that the exception is not an IOException, ensuring the exception type is correct and not from an unrelated hierarchy.

This approach avoids manual try-catch blocks and uses clear assertions with messages for easy debugging.

Common Mistakes - 3 Pitfalls
{'mistake': 'Catching exceptions manually inside the test method instead of using assertThrows', 'why_bad': 'This makes tests longer, harder to read, and can miss assertion failures if exceptions are not properly handled.', 'correct_approach': "Use JUnit 5's assertThrows to automatically check for exceptions."}
Checking exception type with getClass() equality instead of instanceof
Not providing assertion messages
Bonus Challenge

Now add tests to verify that processData throws NullPointerException for empty string input and check its exception hierarchy

Show Hint