Skip to content

Instantly share code, notes, and snippets.

@wkorando
Last active March 3, 2019 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wkorando/48759bde824d7664b3435de832455db2 to your computer and use it in GitHub Desktop.
Save wkorando/48759bde824d7664b3435de832455db2 to your computer and use it in GitHub Desktop.
public class TestHandleExceptionsJUnit5 {
@Test
public void testExceptionHandling() {
Exception e = assertThrows(SpecificException.class, () -> onlyThrowsExceptions());
assertEquals("An exception was thrown!", e.getMessage());
}
@Test
public void testExceptionHandlingFailWrongExceptionType() {
assertThrows(Exception.class, () -> doesntThrowExceptions(), "Wrong exception type thrown!");
}
@Test
public void testExceptionHandlingFailNoExceptionThrown() {
assertThrows(SpecificException.class, () -> doesntThrowExceptions(), "An exception wasn't thrown!");
}
public void onlyThrowsExceptions() throws SpecificException {
throw new SpecificException("An exception was thrown!");
}
public void doesntThrowExceptions() {
//do nothing
}
public class SpecificException extends Exception{
public SpecificException(String message) {
super(message);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment