In the latest snapshot build of JUnit 4.7, we’ve re-implemented expected exceptions using Interceptors. Share and Enjoy!
public static class HasExpectedException {
@Interceptor
public ExpectedException thrown= new ExpectedException();
@Test
public void throwsNothing() {
}
@Test
public void throwsNullPointerException() {
thrown.expect(NullPointerException.class);
throw new NullPointerException();
}
@Test
public void throwsNullPointerExceptionWithMessage() {
thrown.expect(NullPointerException.class);
thrown.expectMessage("happened?");
throw new NullPointerException("What happened?");
}
}



September 2nd, 2009 at 10:08 pm
[…] Interceptors in JUnit 2 JUnit 4.7: Interceptors: expected exceptions Possibly related posts: (automatically generated)Unit Test, JUnit, MockObject … Posted by […]