JUnit 4.7: Interceptors: expected exceptions

Written on Tuesday, May 26th, 2009 at 11:49 pm by David Saff
Filed under Uncategorized.

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?");
		}
	}

One Response to “JUnit 4.7: Interceptors: expected exceptions”

  1. Functional HTTP testing revisited using JUnit 4.7 Interceptors « Alistair Israel Says:

    […] Interceptors in JUnit 2 JUnit 4.7: Interceptors: expected exceptions Possibly related posts: (automatically generated)Unit Test, JUnit, MockObject … Posted by […]

Leave a Reply

You must be logged in to post a comment.