-
Notifications
You must be signed in to change notification settings - Fork 71
/
ExpectedToFailExtension.java
76 lines (62 loc) · 2.72 KB
/
ExpectedToFailExtension.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
/*
* Copyright 2016-2022 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v20.html
*/
package org.junitpioneer.jupiter;
import static org.junit.jupiter.api.Assertions.fail;
import java.lang.reflect.Method;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.InvocationInterceptor;
import org.junit.jupiter.api.extension.ReflectiveInvocationContext;
import org.junit.platform.commons.support.AnnotationSupport;
import org.opentest4j.TestAbortedException;
class ExpectedToFailExtension implements Extension, InvocationInterceptor {
@Override
public void interceptTestMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext,
ExtensionContext extensionContext) throws Throwable {
invokeAndInvertResult(invocation, extensionContext);
}
private static <T> T invokeAndInvertResult(Invocation<T> invocation, ExtensionContext extensionContext)
throws Throwable {
try {
invocation.proceed();
// at this point, the invocation succeeded, so we'd want to call `fail(...)`,
// but that would get handled by the following `catch` and so it's easier
// to instead fall through to a `fail(...)` after the `catch` block
}
catch (Throwable t) {
if (shouldPreserveException(t)) {
throw t;
}
String message = getExpectedToFailAnnotation(extensionContext).value();
if (message.isEmpty()) {
message = "Test marked as temporarily 'expected to fail' failed as expected";
}
throw new TestAbortedException(message, t);
}
return fail("Test marked as 'expected to fail' succeeded; remove @ExpectedToFail from it");
}
/**
* Returns whether the exception should be preserved and reported as is instead
* of considering it an 'expected to fail' exception.
*
* <p>This method is used for exceptions that abort test execution and should
* have higher precedence than aborted exceptions thrown by this extension.
*/
private static boolean shouldPreserveException(Throwable t) {
// Note: Ideally would use the same logic JUnit uses to determine if exception is aborting
// execution, see its class OpenTest4JAndJUnit4AwareThrowableCollector
return TestAbortedException.class.isInstance(t);
}
private static ExpectedToFail getExpectedToFailAnnotation(ExtensionContext context) {
return AnnotationSupport
.findAnnotation(context.getRequiredTestMethod(), ExpectedToFail.class)
.orElseThrow(() -> new IllegalStateException("@ExpectedToFail is missing."));
}
}