-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathExpectedExceptionExtension.java
84 lines (71 loc) · 3.17 KB
/
ExpectedExceptionExtension.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package org.codefx.demo.junit5;
import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
import org.junit.jupiter.api.extension.TestExecutionExceptionHandler;
import org.opentest4j.AssertionFailedError;
import java.util.Optional;
import static org.junit.platform.commons.support.AnnotationSupport.findAnnotation;
public class ExpectedExceptionExtension implements TestExecutionExceptionHandler, AfterTestExecutionCallback {
/*
* This extension implements the exception handler callback to compare the thrown exception
* to what was expected. The after test callback (which is called later) builds on
* the results of that check and fails the test if an exception was expected but not thrown.
*/
private static final Namespace NAMESPACE = Namespace.create("org", "codefx", "ExpectedException");
private static final String KEY = "ExceptionWasT";
@Override
public void handleTestExecutionException(ExtensionContext context, Throwable throwable) throws Throwable {
boolean throwableMatchesExpectedException =
expectedException(context)
.filter(expected -> expected.isInstance(throwable))
.isPresent();
// in the `afterTestExecution` callback we have to pass or fail the test
// depending on whether the exception was thrown or not;
// to do that we need to register whether the exception was thrown;
// (NOTE that if no exception was thrown, NOTHING is registered)
if (throwableMatchesExpectedException)
storeExceptionStatus(context, EXCEPTION.WAS_THROWN_AS_EXPECTED);
else {
// this extension is not in charge of the throwable, so we need to rethrow;
storeExceptionStatus(context, EXCEPTION.WAS_THROWN_NOT_AS_EXPECTED);
throw throwable;
}
}
@Override
public void afterTestExecution(ExtensionContext context) throws Exception {
switch(loadExceptionStatus(context)) {
case WAS_NOT_THROWN:
expectedException(context)
.map(expected -> new AssertionFailedError("Expected exception " + expected + " was not thrown.", expected, null))
.ifPresent(ex -> { throw ex; });
case WAS_THROWN_AS_EXPECTED:
// the exception was thrown as expected so there is nothing to do
case WAS_THROWN_NOT_AS_EXPECTED:
// an exception was thrown but of the wrong type;
// it was rethrown in `handleTestExecutionException`
// so there is nothing to do here
}
}
private static Optional<? extends Class<? extends Throwable>> expectedException(ExtensionContext context) {
return context.getElement()
.flatMap(el -> findAnnotation(el, Test.class))
.map(Test::expected)
.filter(exceptionType -> exceptionType != Test.None.class);
}
private static void storeExceptionStatus(ExtensionContext context, EXCEPTION thrown) {
context.getStore(NAMESPACE).put(KEY, thrown);
}
private static EXCEPTION loadExceptionStatus(ExtensionContext context) {
EXCEPTION thrown = context.getStore(NAMESPACE).get(KEY, EXCEPTION.class);
if (thrown == null)
return EXCEPTION.WAS_NOT_THROWN;
else
return thrown;
}
private enum EXCEPTION {
WAS_NOT_THROWN,
WAS_THROWN_AS_EXPECTED,
WAS_THROWN_NOT_AS_EXPECTED,
}
}