Skip to content

Instantly share code, notes, and snippets.

@maartenl
Created August 27, 2015 06:59
Show Gist options
  • Save maartenl/73f4b8aad4cd41a83ff1 to your computer and use it in GitHub Desktop.
Save maartenl/73f4b8aad4cd41a83ff1 to your computer and use it in GitHub Desktop.
Testing for Exceptions in the new JUnit.
/**
*
* @author mrbear
*/
public class AddressJUnitTest
{
@Rule
public ExpectedException nameMayNotBeNull = ExpectedException.none();
@Test
public void testWithNullpointerExceptionInJUnit()
{
nameMayNotBeNull.expect(NullPointerException.class);
nameMayNotBeNull.expectMessage("the name may not be null");
Address mrbear = new Address("Mr. Bear");
mrbear.setName(null);
assertThat(mrbear.getName(), not(nullValue()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment