Explain Codes LogoExplain Codes Logo

How do I assert my exception message with JUnit Test annotation?

java
assertions
junit
exception-handling
Anton ShumikhinbyAnton Shumikhin·Oct 2, 2024
TLDR

Use JUnit's assertThrows to capture exception and validate its message:

YourException e = assertThrows(YourException.class, () -> { // trigger exception }); assertEquals("Expected message", e.getMessage());

Replace YourException with the actual exception class and "Expected message" with the message you're asserting. This ensures the correct exception is thrown with the exact message you're testing for.

Why assertThrows is brilliant

assertThrows lets you write assertive, clear code. It has an undeniable brevity, and plays nice with lambda expressions, ensuring your test code remains sleek and understandable.

Flexing with ExpectedException in JUnit 4

ExpectedException presents an alternative route to asserting exceptions and their messages. It even prepares for exceptions before the test execution.

Setting up an ExpectedException rule

@Rule public ExpectedException exceptionRule = ExpectedException.none(); // "I'm simply a rule, living in a world of chaos."

Specifying the expected exception and message

@Test public void shouldThrowSpecificExceptionWithMessage() { exceptionRule.expect(YourException.class); // "I'm expecting a special guest tonight." exceptionRule.expectMessage("Expected message"); // "Trust me, you'll know when you see...err hear it." // the code that triggers the exception }

Other cases: try-catch

Sometimes, an exception is just the start of your investigation. On these occasions, a try-catch block comes handy:

@Test public void testForExceptionDetails() { try { // code that triggers exception fail("Expected YourExceptionClass not thrown - plot twist!"); } catch (YourExceptionClass e) { assertEquals("Expected message", e.getMessage()); // More assertions here; because, why not? } }

Staying current: Knowledge is power!

Stay current, stay cool. Check out the latest features and updates from official JUnit resources:

  1. The JUnit 5 User Guide for needed updates.
  2. The JUnit 5 Release Notes to stay hip with the changes.

More exception handling techniques to remember

Not expecting particular type of exception? No worries.

@Test(expected = YourExceptionClass.class) public void shouldThrowException() { // code that should throw YourExceptionClass }

Confirming no exceptions? Easy peasy!

@Test public void shouldNotThrowException() { exceptionRule.expect(ExpectedException.none()); // "The only exception I want to see, is absolutely nothing." // code that shouldn't throw any exception }

Customise failure messages? Piece of cake!

try { // code expected to throw an exception fail("Oops, my pebble didn't skip when I expected it to!"); } catch (YourExceptionClass e) { // handle the exception }

Brace for unexpected exceptions? Always!

@Test public void shouldOnlyThrowSpecificException() { try { // code that might throw several types of exceptions } catch (YourExceptionClass e) { // assertion for the expected exception } catch (Exception unexpected) { fail("An unexpected wild exception appeared! " + unexpected.getMessage()); } }