In this TDD Java JUnit example I show you how to test your java exceptions by coding Java JUnit Rules. Specifically implementing the Junit Rule ExpectedException combined with Hamcrest matchers to assert finer grained details and expressions of intent.
How to assert the exceptions cause. This can be useful if you are working in a codebase that catches the real exception, creates a new exception and places the real exception as its cause. Wrapper exceptions.
How to assert the excetions message with hamcrest matchers.
I also demo how to expect basic exceptions from a simple JUnit test.
Don't forget to subscribe for more regular great tech content!