org.junit.jupiter.api.extension.ReflectiveInvocationContext Java Examples

The following examples show how to use org.junit.jupiter.api.extension.ReflectiveInvocationContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AllureJunit5.java    From allure-java with Apache License 2.0 6 votes vote down vote up
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;
    }
}
 
Example #2
Source File: QuarkusUnitTest.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public void interceptAfterEachMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext,
        ExtensionContext extensionContext) throws Throwable {
    if (assertException == null) {
        runExtensionMethod(invocationContext);
        invocation.skip();
    } else {
        invocation.proceed();
    }
}
 
Example #3
Source File: JaasExtension.java    From taskana with Apache License 2.0 5 votes vote down vote up
@Override
public void interceptAfterAllMethod(
    Invocation<Void> invocation,
    ReflectiveInvocationContext<Method> invocationContext,
    ExtensionContext extensionContext) {
  extractAccessIdAndPerformInvocation(invocation, invocationContext.getExecutable());
}
 
Example #4
Source File: JaasExtension.java    From taskana with Apache License 2.0 5 votes vote down vote up
@Override
public void interceptAfterEachMethod(
    Invocation<Void> invocation,
    ReflectiveInvocationContext<Method> invocationContext,
    ExtensionContext extensionContext) {
  extractAccessIdAndPerformInvocation(invocation, invocationContext.getExecutable());
}
 
Example #5
Source File: JaasExtension.java    From taskana with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #6
Source File: JaasExtension.java    From taskana with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #7
Source File: JaasExtension.java    From taskana with Apache License 2.0 5 votes vote down vote up
@Override
public void interceptBeforeEachMethod(
    Invocation<Void> invocation,
    ReflectiveInvocationContext<Method> invocationContext,
    ExtensionContext extensionContext) {
  extractAccessIdAndPerformInvocation(invocation, invocationContext.getExecutable());
}
 
Example #8
Source File: JaasExtension.java    From taskana with Apache License 2.0 5 votes vote down vote up
@Override
public void interceptBeforeAllMethod(
    Invocation<Void> invocation,
    ReflectiveInvocationContext<Method> invocationContext,
    ExtensionContext extensionContext) {
  extractAccessIdAndPerformInvocation(invocation, invocationContext.getExecutable());
}
 
Example #9
Source File: JaasExtension.java    From taskana with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T interceptTestClassConstructor(
    Invocation<T> invocation,
    ReflectiveInvocationContext<Constructor<T>> invocationContext,
    ExtensionContext extensionContext) {
  return extractAccessIdAndPerformInvocation(invocation, invocationContext.getExecutable());
}
 
Example #10
Source File: AllureJunit5.java    From allure-java with Apache License 2.0 5 votes vote down vote up
@Override
public void interceptAfterEachMethod(
        final Invocation<Void> invocation,
        final ReflectiveInvocationContext<Method> invocationContext,
        final ExtensionContext extensionContext) throws Throwable {
    processFixture(TEAR_DOWN, invocation, invocationContext, extensionContext);
}
 
Example #11
Source File: AllureJunit5.java    From allure-java with Apache License 2.0 5 votes vote down vote up
@Override
public void interceptBeforeEachMethod(
        final Invocation<Void> invocation,
        final ReflectiveInvocationContext<Method> invocationContext,
        final ExtensionContext extensionContext) throws Throwable {
    processFixture(PREPARE, invocation, invocationContext, extensionContext);
}
 
Example #12
Source File: AllureJunit5.java    From allure-java with Apache License 2.0 5 votes vote down vote up
@Override
public void interceptAfterAllMethod(
        final Invocation<Void> invocation,
        final ReflectiveInvocationContext<Method> invocationContext,
        final ExtensionContext extensionContext) throws Throwable {
    processFixture(TEAR_DOWN, invocation, invocationContext, extensionContext);
}
 
Example #13
Source File: AllureJunit5.java    From allure-java with Apache License 2.0 5 votes vote down vote up
@Override
public void interceptBeforeAllMethod(
        final Invocation<Void> invocation,
        final ReflectiveInvocationContext<Method> invocationContext,
        final ExtensionContext extensionContext) throws Throwable {
    processFixture(PREPARE, invocation, invocationContext, extensionContext);
}
 
Example #14
Source File: QuarkusUnitTest.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public void interceptTestTemplateMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext,
        ExtensionContext extensionContext) throws Throwable {
    if (assertException == null) {
        runExtensionMethod(invocationContext);
    }
    invocation.skip();
}
 
Example #15
Source File: QuarkusUnitTest.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public void interceptTestMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext,
        ExtensionContext extensionContext) throws Throwable {
    if (assertException == null) {
        runExtensionMethod(invocationContext);
    }
    invocation.skip();
}
 
Example #16
Source File: QuarkusUnitTest.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public void interceptAfterAllMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext,
        ExtensionContext extensionContext) throws Throwable {
    if (assertException == null) {
        runExtensionMethod(invocationContext);
    }
    invocation.skip();
}
 
Example #17
Source File: QuarkusTestExtension.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #18
Source File: QuickPerfTestExtension.java    From quickperf with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example #19
Source File: QuickPerfTestExtension.java    From quickperf with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #20
Source File: QuarkusTestExtension.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #21
Source File: QuarkusTestExtension.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #22
Source File: QuarkusTestExtension.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #23
Source File: QuarkusTestExtension.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #24
Source File: QuarkusTestExtension.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #25
Source File: QuickPerfTestExtension.java    From quickperf with Apache License 2.0 5 votes vote down vote up
@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);

}
 
Example #26
Source File: QuarkusTestExtension.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #27
Source File: QuarkusTestExtension.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #28
Source File: QuarkusProdModeTest.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Override
public void interceptBeforeAllMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext,
        ExtensionContext extensionContext) throws Throwable {
    doIntercept(invocation);
}
 
Example #29
Source File: QuarkusProdModeTest.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Override
public void interceptBeforeEachMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext,
        ExtensionContext extensionContext) throws Throwable {
    doIntercept(invocation);
}
 
Example #30
Source File: JaasExtension.java    From taskana with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <T> T interceptTestFactoryMethod(
    Invocation<T> invocation,
    ReflectiveInvocationContext<Method> invocationContext,
    ExtensionContext extensionContext) {
  WithAccessIds annotation = invocationContext.getExecutable().getAnnotation(WithAccessIds.class);
  if (annotation != null) {
    // our goal is to run each test returned from the test factory X times. X is the amount of
    // WithAccessId annotations. In order to achieve this we are wrapping the result from the
    // factory (the returning tests) in a dynamicContainer for each accessId. Since we don't know
    // what the factory will return we have to check for every possible return type. All possible
    // return types can be found here:
    // https://junit.org/junit5/docs/current/user-guide/#writing-tests-dynamic-tests
    // After checking each return type we abuse the return type of T and hardly change it to
    // Stream<DynamicContainer> no matter what the factory returns. This return type is allowed
    // per definition (See link above), but is not the type T. Hence we have an unchecked cast at
    // the end to keep the compiler happy...

    // we are using the first annotation to run the factory method with.
    T factoryResult = performInvocationWithAccessId(invocation, annotation.value()[0]);

    Iterable<DynamicNode> newChildrenForDynamicContainer;
    // TestFactory must have one of the following return types. See link above for further details
    if (factoryResult instanceof DynamicNode) {
      newChildrenForDynamicContainer = Collections.singleton((DynamicNode) factoryResult);
    } else if (factoryResult instanceof Stream) {
      Stream<DynamicNode> nodes = (Stream<DynamicNode>) factoryResult;
      newChildrenForDynamicContainer = nodes.collect(Collectors.toList());
    } else if (factoryResult instanceof Iterable) {
      newChildrenForDynamicContainer = (Iterable<DynamicNode>) factoryResult;
    } else if (factoryResult instanceof Iterator) {
      newChildrenForDynamicContainer = () -> (Iterator<DynamicNode>) factoryResult;
    } else if (factoryResult instanceof DynamicNode[]) {
      newChildrenForDynamicContainer = Arrays.asList((DynamicNode[]) factoryResult);
    } else {
      throw new SystemException(
          String.format(
              "Testfactory '%s' did not return a proper type",
              invocationContext.getExecutable().getName()));
    }

    // Currently a DynamicContainer has children from this type: Stream<DynamicNode>
    // Because of this the children can only be extracted once (Streams can only be operated
    // once). This is obviously not ok since we want to execute each node X times. So we have to
    // manually persist all children recursively to extract them X times...
    Map<String, List<DynamicNode>> childrenMap = new HashMap<>();
    persistDynamicContainerChildren(newChildrenForDynamicContainer, childrenMap);

    Function<WithAccessId, DynamicContainer> wrapTestsInDynamicContainer =
        accessId ->
            DynamicContainer.dynamicContainer(
                getDisplayNameForAccessId(accessId),
                StreamSupport.stream(newChildrenForDynamicContainer.spliterator(), false)
                    .map(x -> duplicateDynamicNode(x, childrenMap)));

    Store store = getStore(extensionContext);
    return (T)
        Stream.of(annotation.value())
            .peek(a -> store.put(ACCESS_IDS_STORE_KEY, a))
            .map(wrapTestsInDynamicContainer);
  }

  return extractAccessIdAndPerformInvocation(invocation, invocationContext.getExecutable());
}