TesterHQ - The Evil Tester Blog Aggregator

Jun 10, 2013 - 1 minute read - Java For Testers JUnit

JUnit ExpectedException Rule

Original Blog Posting on [blog.javafortesters.com]

JUnit has a new way of testing for Exceptions.
Back in the day, we used to add the expected parameter to the @Test annotation.

@Test(expected = InvalidPassword.class)

But now, JUnit has a new @Rule

public ExpectedException expected = ExpectedException.none();

And you configure the ExpectedException in the @Test method:

expected.expectMessage("> 6 chars");

Very useful if you want finer grained control over your Exception testing.

You can find out more on the JUnit github.