Created
May 16, 2018 12:16
-
-
Save jon-ruckwood/02a4a931fea0c246eb4d90f9ee14aa39 to your computer and use it in GitHub Desktop.
Mini JUnit5 extension simulating the useful expected exception functionality of JUnit4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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