Skip to main content

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

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

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.

- in Java For Testers JUnit

You will need a Github account to comment. Or you can contact me with your comment.

I reserve the right to delete spam comments e.g. if your comment adds no value and its purpose is simply to create a backlink to another site offering training, or courses, or etc.