下面列出了怎么用org.junit.jupiter.api.extension.ReflectiveInvocationContext的API类实例代码及写法,或者点击链接到github查看源代码。
protected void processFixture(final String type,
final Invocation<Void> invocation,
final ReflectiveInvocationContext<Method> invocationContext,
final ExtensionContext extensionContext) throws Throwable {
final String uuid = UUID.randomUUID().toString();
try {
extensionContext.publishReportEntry(buildStartEvent(
type,
uuid,
invocationContext.getExecutable()
));
invocation.proceed();
extensionContext.publishReportEntry(buildStopEvent(
type,
uuid
));
} catch (Throwable throwable) {
extensionContext.publishReportEntry(buildFailureEvent(
type,
uuid,
throwable
));
throw throwable;
}
}
@Override
public void interceptTestMethod( Invocation<Void> invocation
, ReflectiveInvocationContext<Method> invocationContext
, ExtensionContext extensionContext) throws Throwable {
if (testExecutionContext.isQuickPerfDisabled()) {
invocation.proceed();
return;
}
if(SystemProperties.TEST_CODE_EXECUTING_IN_NEW_JVM.evaluate()) {
executeTestMethodInNewJvmAndRecordPerformance(invocation, invocationContext);
return;
}
JvmOrTestIssue jvmOrTestIssue =
executeTestMethodAndRecordPerformance(invocation, invocationContext);
SetOfAnnotationConfigs testAnnotationConfigs = quickPerfConfigs.getTestAnnotationConfigs();
Collection<PerfIssuesToFormat> groupOfPerfIssuesToFormat
= perfIssuesEvaluator.evaluatePerfIssuesIfNoJvmIssue(testAnnotationConfigs
, testExecutionContext
, jvmOrTestIssue);
testExecutionContext.cleanResources();
quickPerfReporter.report(jvmOrTestIssue
, groupOfPerfIssuesToFormat
, testExecutionContext);
}
private void executeTestMethodInNewJvmAndRecordPerformance(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext) throws IllegalAccessException, InvocationTargetException {
Object[] args = invocationContext.getArguments().toArray();
Object target = invocationContext.getTarget().orElse(null);
Method method = makeAccessible(invocationContext.getExecutable());
invocation.skip();//skip the invocation as we directly invoke the test method
performanceRecording.start(testExecutionContext);
try {
//directly invoke the method to lower the interaction between JUnit, other extensions and QuickPerf.
method.invoke(target, args);
} finally {
performanceRecording.stop(testExecutionContext);
}
}
private JvmOrTestIssue executeTestMethodAndRecordPerformance(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext) {
if (testExecutionContext.testExecutionUsesTwoJVMs()) {
Method testMethod = invocationContext.getExecutable();
JvmOrTestIssue jvmOrTestIssue = executeTestMethodInNewJwm(testMethod);
tryToSkipInvocation(invocation); // because the test method is invoked directly inside the 'newJvmTestLauncher'
return jvmOrTestIssue;
}
TestIssue testIssue = executeTestMethodAndRecordPerformanceInSameJvm(invocation);
return JvmOrTestIssue.buildFrom(testIssue);
}
@Override
public void interceptBeforeAllMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext,
ExtensionContext extensionContext) throws Throwable {
if (isNativeTest(extensionContext)) {
invocation.proceed();
return;
}
ensureStarted(extensionContext);
if (failedBoot) {
throwBootFailureException();
return;
}
runExtensionMethod(invocationContext, extensionContext);
invocation.skip();
}
@Override
public <T> T interceptTestClassConstructor(Invocation<T> invocation,
ReflectiveInvocationContext<Constructor<T>> invocationContext, ExtensionContext extensionContext) throws Throwable {
if (isNativeTest(extensionContext)) {
return invocation.proceed();
}
T result;
ClassLoader old = Thread.currentThread().getContextClassLoader();
Class<?> requiredTestClass = extensionContext.getRequiredTestClass();
try {
Thread.currentThread().setContextClassLoader(requiredTestClass.getClassLoader());
result = invocation.proceed();
} catch (NullPointerException e) {
throw new RuntimeException(
"When using constructor injection in a test, the only legal operation is to assign the constructor values to fields. Offending class is "
+ requiredTestClass,
e);
} finally {
Thread.currentThread().setContextClassLoader(old);
}
ExtensionState state = ensureStarted(extensionContext);
if (failedBoot) {
return result;
}
// We do this here as well, because when @TestInstance(Lifecycle.PER_CLASS) is used on a class,
// interceptTestClassConstructor is called before beforeAll, meaning that the TCCL will not be set correctly
// (for any test other than the first) unless this is done
old = null;
if (runningQuarkusApplication != null) {
old = setCCL(runningQuarkusApplication.getClassLoader());
}
initTestState(extensionContext, state);
if (old != null) {
setCCL(old);
}
return result;
}
@Override
public void interceptBeforeEachMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext,
ExtensionContext extensionContext) throws Throwable {
if (isNativeTest(extensionContext)) {
invocation.proceed();
return;
}
runExtensionMethod(invocationContext, extensionContext);
invocation.skip();
}
@Override
public void interceptTestMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext,
ExtensionContext extensionContext) throws Throwable {
if (isNativeTest(extensionContext)) {
invocation.proceed();
return;
}
runExtensionMethod(invocationContext, extensionContext);
invocation.skip();
}
@Override
public void interceptTestTemplateMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext,
ExtensionContext extensionContext) throws Throwable {
if (isNativeTest(extensionContext)) {
invocation.proceed();
return;
}
runExtensionMethod(invocationContext, extensionContext);
invocation.skip();
}
@Override
public <T> T interceptTestFactoryMethod(Invocation<T> invocation,
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
if (isNativeTest(extensionContext)) {
return invocation.proceed();
}
T result = (T) runExtensionMethod(invocationContext, extensionContext);
invocation.skip();
return result;
}
@Override
public void interceptAfterEachMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext,
ExtensionContext extensionContext) throws Throwable {
if (isNativeTest(extensionContext)) {
invocation.proceed();
return;
}
runExtensionMethod(invocationContext, extensionContext);
invocation.skip();
}
@Override
public void interceptAfterAllMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext,
ExtensionContext extensionContext) throws Throwable {
if (isNativeTest(extensionContext)) {
invocation.proceed();
return;
}
runExtensionMethod(invocationContext, extensionContext);
invocation.skip();
}
@Override
public void interceptAfterEachMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext,
ExtensionContext extensionContext) throws Throwable {
if (assertException == null) {
runExtensionMethod(invocationContext);
invocation.skip();
} else {
invocation.proceed();
}
}
@Override
public void interceptAfterAllMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext,
ExtensionContext extensionContext) throws Throwable {
if (assertException == null) {
runExtensionMethod(invocationContext);
}
invocation.skip();
}
@Override
public void interceptTestMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext,
ExtensionContext extensionContext) throws Throwable {
if (assertException == null) {
runExtensionMethod(invocationContext);
}
invocation.skip();
}
@Override
public void interceptTestTemplateMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext,
ExtensionContext extensionContext) throws Throwable {
if (assertException == null) {
runExtensionMethod(invocationContext);
}
invocation.skip();
}
@Override
public void interceptBeforeAllMethod(
final Invocation<Void> invocation,
final ReflectiveInvocationContext<Method> invocationContext,
final ExtensionContext extensionContext) throws Throwable {
processFixture(PREPARE, invocation, invocationContext, extensionContext);
}
@Override
public void interceptAfterAllMethod(
final Invocation<Void> invocation,
final ReflectiveInvocationContext<Method> invocationContext,
final ExtensionContext extensionContext) throws Throwable {
processFixture(TEAR_DOWN, invocation, invocationContext, extensionContext);
}
@Override
public void interceptBeforeEachMethod(
final Invocation<Void> invocation,
final ReflectiveInvocationContext<Method> invocationContext,
final ExtensionContext extensionContext) throws Throwable {
processFixture(PREPARE, invocation, invocationContext, extensionContext);
}
@Override
public void interceptAfterEachMethod(
final Invocation<Void> invocation,
final ReflectiveInvocationContext<Method> invocationContext,
final ExtensionContext extensionContext) throws Throwable {
processFixture(TEAR_DOWN, invocation, invocationContext, extensionContext);
}
@Override
public <T> T interceptTestClassConstructor(
Invocation<T> invocation,
ReflectiveInvocationContext<Constructor<T>> invocationContext,
ExtensionContext extensionContext) {
return extractAccessIdAndPerformInvocation(invocation, invocationContext.getExecutable());
}
@Override
public void interceptBeforeAllMethod(
Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext,
ExtensionContext extensionContext) {
extractAccessIdAndPerformInvocation(invocation, invocationContext.getExecutable());
}
@Override
public void interceptBeforeEachMethod(
Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext,
ExtensionContext extensionContext) {
extractAccessIdAndPerformInvocation(invocation, invocationContext.getExecutable());
}
@Override
public void interceptTestMethod(
Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext,
ExtensionContext extensionContext) {
if (isAnnotated(invocationContext.getExecutable(), WithAccessIds.class)) {
throw new JUnitException("Please use @TestTemplate instead of @Test for multiple accessIds");
}
extractAccessIdAndPerformInvocation(invocation, invocationContext.getExecutable());
}
@Override
public void interceptTestTemplateMethod(
Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext,
ExtensionContext extensionContext) {
WithAccessId accessId =
getStore(extensionContext).get(ACCESS_IDS_STORE_KEY, WithAccessId.class);
performInvocationWithAccessId(invocation, accessId);
}
@Override
public void interceptAfterEachMethod(
Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext,
ExtensionContext extensionContext) {
extractAccessIdAndPerformInvocation(invocation, invocationContext.getExecutable());
}
@Override
public void interceptAfterAllMethod(
Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext,
ExtensionContext extensionContext) {
extractAccessIdAndPerformInvocation(invocation, invocationContext.getExecutable());
}
@Override
public void interceptBeforeAllMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext,
ExtensionContext extensionContext) throws Throwable {
doIntercept(invocation);
}
@Override
public void interceptBeforeEachMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext,
ExtensionContext extensionContext) throws Throwable {
doIntercept(invocation);
}
@Override
public void interceptTestMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext,
ExtensionContext extensionContext) throws Throwable {
doIntercept(invocation);
}