Skip to content

Instantly share code, notes, and snippets.

@jon-ruckwood
Created May 16, 2018 12:16
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 jon-ruckwood/02a4a931fea0c246eb4d90f9ee14aa39 to your computer and use it in GitHub Desktop.
Save jon-ruckwood/02a4a931fea0c246eb4d90f9ee14aa39 to your computer and use it in GitHub Desktop.
Mini JUnit5 extension simulating the useful expected exception functionality of JUnit4
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface IgnoreException {
Class<? extends Throwable>[] value();
}
public class IgnoreExceptionExtension implements TestExecutionExceptionHandler {
@Override
public void handleTestExecutionException(ExtensionContext context, Throwable throwable) throws Throwable {
IgnoreException annotation = context.getRequiredTestMethod().getAnnotation(IgnoreException.class);
if (annotation != null) {
Optional<?> o = Arrays.stream(annotation.value())
.filter(c -> c.isAssignableFrom(throwable.getClass()))
.findFirst();
if (!o.isPresent()) {
throw throwable;
}
}
}
}
class FooTest {
@Test
@IgnoreException({IllegalArgumentException.class, IllegalStateException.class})
void doTheRoar() {
//...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment