Monday, 10 June 2013

JUnit ExpectedException Rule

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
@Rulepublic ExpectedException expected = ExpectedException.none();
And you configure the ExpectedException in the @Test method:
expected.expect(InvalidPassword.class);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.

No comments:

Post a Comment