org.junit.jupiter.api.extension.ExtensionContext.Store Java Examples

The following examples show how to use org.junit.jupiter.api.extension.ExtensionContext.Store. 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 Project: bobcat   Author: Cognifide   File: GuiceExtension.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Create {@link Injector} or get existing one from test context
 */
private static Optional<Injector> getOrCreateInjector(ExtensionContext context)
    throws NoSuchMethodException, InstantiationException, IllegalAccessException,
    InvocationTargetException {

  Optional<AnnotatedElement> optionalAnnotatedElement = context.getElement();
  if (!optionalAnnotatedElement.isPresent()) {
    return Optional.empty();
  }

  AnnotatedElement element = optionalAnnotatedElement.get();
  Store store = context.getStore(NAMESPACE);

  Injector injector = store.get(element, Injector.class);
  if (injector == null) {
    injector = createInjector(context);
    store.put(element, injector);
  }

  return Optional.of(injector);
}
 
Example #2
Source Project: testcontainers-java   Author: testcontainers   File: TestcontainersExtension.java    License: MIT License 6 votes vote down vote up
@Override
public void beforeAll(ExtensionContext context) {
    Class<?> testClass = context.getTestClass()
        .orElseThrow(() -> new ExtensionConfigurationException("TestcontainersExtension is only supported for classes."));

    Store store = context.getStore(NAMESPACE);
    List<StoreAdapter> sharedContainersStoreAdapters = findSharedContainers(testClass);

    sharedContainersStoreAdapters.forEach(adapter -> store.getOrComputeIfAbsent(adapter.getKey(), k -> adapter.start()));

    List<TestLifecycleAware> lifecycleAwareContainers = sharedContainersStoreAdapters
        .stream()
        .filter(this::isTestLifecycleAware)
        .map(lifecycleAwareAdapter -> (TestLifecycleAware) lifecycleAwareAdapter.container)
        .collect(toList());

    store.put(SHARED_LIFECYCLE_AWARE_CONTAINERS, lifecycleAwareContainers);
    signalBeforeTestToContainers(lifecycleAwareContainers, testDescriptionFrom(context));
}
 
Example #3
Source Project: GitToolBox   Author: zielu   File: IdeaMocksExtension.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void beforeEach(ExtensionContext context) {
  IdeaMocksImpl ideaMocks = new IdeaMocksImpl();
  Project project = mock(Project.class);
  MessageBus messageBus = mock(MessageBus.class);
  when(project.getMessageBus()).thenReturn(messageBus);
  when(messageBus.syncPublisher(any(Topic.class))).thenAnswer(invocation -> {
    Topic topic = invocation.getArgument(0);
    Class<?> listenerClass = topic.getListenerClass();
    if (ideaMocks.hasMockListener(listenerClass)) {
      return ideaMocks.getMockListener(listenerClass);
    } else {
      return ideaMocks.mockListener(listenerClass);
    }
  });
  Store store = context.getStore(NS);
  ParameterHolder holder = ParameterHolder.getHolder(store);
  holder.register(Project.class, Suppliers.ofInstance(project));
  holder.register(MessageBus.class, Suppliers.ofInstance(messageBus));
  holder.register(IdeaMocks.class, Suppliers.ofInstance(ideaMocks));
}
 
Example #4
Source Project: spring-analysis-note   Author: Vip-Augus   File: SpringExtension.java    License: MIT License 5 votes vote down vote up
/**
 * Get the {@link TestContextManager} associated with the supplied {@code ExtensionContext}.
 * @return the {@code TestContextManager} (never {@code null})
 */
private static TestContextManager getTestContextManager(ExtensionContext context) {
	Assert.notNull(context, "ExtensionContext must not be null");
	Class<?> testClass = context.getRequiredTestClass();
	Store store = getStore(context);
	return store.getOrComputeIfAbsent(testClass, TestContextManager::new, TestContextManager.class);
}
 
Example #5
Source Project: spring-analysis-note   Author: Vip-Augus   File: DisabledIfConditionTests.java    License: MIT License 5 votes vote down vote up
private ExtensionContext buildExtensionContext(String methodName) {
	Class<?> testClass = SpringTestCase.class;
	Method method = ReflectionUtils.findMethod(getClass(), methodName);
	Store store = mock(Store.class);
	given(store.getOrComputeIfAbsent(any(), any(), any())).willReturn(new TestContextManager(testClass));

	ExtensionContext extensionContext = mock(ExtensionContext.class);
	given(extensionContext.getTestClass()).willReturn(Optional.of(testClass));
	given(extensionContext.getElement()).willReturn(Optional.of(method));
	given(extensionContext.getStore(any())).willReturn(store);
	return extensionContext;
}
 
Example #6
Source Project: java-technology-stack   Author: codeEngraver   File: SpringExtension.java    License: MIT License 5 votes vote down vote up
/**
 * Get the {@link TestContextManager} associated with the supplied {@code ExtensionContext}.
 * @return the {@code TestContextManager} (never {@code null})
 */
private static TestContextManager getTestContextManager(ExtensionContext context) {
	Assert.notNull(context, "ExtensionContext must not be null");
	Class<?> testClass = context.getRequiredTestClass();
	Store store = getStore(context);
	return store.getOrComputeIfAbsent(testClass, TestContextManager::new, TestContextManager.class);
}
 
Example #7
Source Project: java-technology-stack   Author: codeEngraver   File: DisabledIfConditionTests.java    License: MIT License 5 votes vote down vote up
private ExtensionContext buildExtensionContext(String methodName) {
	Class<?> testClass = SpringTestCase.class;
	Method method = ReflectionUtils.findMethod(getClass(), methodName);
	Store store = mock(Store.class);
	when(store.getOrComputeIfAbsent(any(), any(), any())).thenReturn(new TestContextManager(testClass));

	ExtensionContext extensionContext = mock(ExtensionContext.class);
	when(extensionContext.getTestClass()).thenReturn(Optional.of(testClass));
	when(extensionContext.getElement()).thenReturn(Optional.of(method));
	when(extensionContext.getStore(any())).thenReturn(store);
	return extensionContext;
}
 
Example #8
Source Project: hypergraphql   Author: hypergraphql   File: MockitoExtension.java    License: Apache License 2.0 5 votes vote down vote up
private Object getMock(Parameter parameter, ExtensionContext extensionContext) {
    Class<?> mockType = parameter.getType();
    Store mocks = extensionContext.getStore(Namespace.create(MockitoExtension.class, mockType));
    String mockName = getMockName(parameter);

    if (mockName != null) {
        return mocks.getOrComputeIfAbsent(mockName, key -> mock(mockType, mockName));
    }
    else {
        return mocks.getOrComputeIfAbsent(mockType.getCanonicalName(), key -> mock(mockType));
    }
}
 
Example #9
Source Project: intellij-spring-assistant   Author: 1tontech   File: MockitoExtension.java    License: MIT License 5 votes vote down vote up
private Object getMock(Parameter parameter, ExtensionContext extensionContext) {
  Class<?> mockType = parameter.getType();
  Store mocks = extensionContext.getStore(Namespace.create(MockitoExtension.class, mockType));
  String mockName = getMockName(parameter);

  if (mockName != null) {
    return mocks.getOrComputeIfAbsent(mockName, key -> mock(mockType, mockName));
  } else {
    return mocks.getOrComputeIfAbsent(mockType.getCanonicalName(), key -> mock(mockType));
  }
}
 
Example #10
Source Project: taskana   Author: Taskana   File: JaasExtension.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(
    ExtensionContext context) {
  List<WithAccessId> accessIds =
      AnnotationSupport.findRepeatableAnnotations(context.getElement(), WithAccessId.class);
  Store store = getStore(context);
  return accessIds.stream()
      .peek(a -> store.put(ACCESS_IDS_STORE_KEY, a))
      .map(JaasExtensionInvocationContext::new);
}
 
Example #11
Source Project: junit5-extensions   Author: JeffreyFalgout   File: GuiceExtension.java    License: MIT License 5 votes vote down vote up
/**
 * Returns an injector for the given context if and only if the given context has an {@link
 * ExtensionContext#getElement() annotated element}.
 */
private static Optional<Injector> getOrCreateInjector(ExtensionContext context)
    throws NoSuchMethodException,
    InstantiationException,
    IllegalAccessException,
    InvocationTargetException {
  if (!context.getElement().isPresent()) {
    return Optional.empty();
  }
  AnnotatedElement element = context.getElement().get();
  Store store = context.getStore(NAMESPACE);
  Injector injector = store.get(element, Injector.class);
  boolean sharedInjector = isSharedInjector(context);
  Set<Class<? extends Module>> moduleClasses = Collections.emptySet();
  if (injector == null && sharedInjector) {
    moduleClasses = getContextModuleTypes(context);
    injector = INJECTOR_CACHE.get(moduleClasses);
  }
  if (injector == null) {
    injector = createInjector(context);
    store.put(element, injector);
    if (sharedInjector && !moduleClasses.isEmpty()) {
      INJECTOR_CACHE.put(moduleClasses, injector);
    }
  }
  return Optional.of(injector);
}
 
Example #12
Source Project: Mastering-Software-Testing-with-JUnit-5   Author: PacktPublishing   File: MockitoExtension.java    License: MIT License 5 votes vote down vote up
private Object getMock(Parameter parameter,
        ExtensionContext extensionContext) {
    Class<?> mockType = parameter.getType();
    Store mocks = extensionContext
            .getStore(Namespace.create(MockitoExtension.class, mockType));
    String mockName = getMockName(parameter);

    if (mockName != null) {
        return mocks.getOrComputeIfAbsent(mockName,
                key -> mock(mockType, mockName));
    } else {
        return mocks.getOrComputeIfAbsent(mockType.getCanonicalName(),
                key -> mock(mockType));
    }
}
 
Example #13
Source Project: Mastering-Software-Testing-with-JUnit-5   Author: PacktPublishing   File: MockitoExtension.java    License: MIT License 5 votes vote down vote up
private Object getMock(Parameter parameter,
        ExtensionContext extensionContext) {
    Class<?> mockType = parameter.getType();
    Store mocks = extensionContext
            .getStore(Namespace.create(MockitoExtension.class, mockType));
    String mockName = getMockName(parameter);

    if (mockName != null) {
        return mocks.getOrComputeIfAbsent(mockName,
                key -> mock(mockType, mockName));
    } else {
        return mocks.getOrComputeIfAbsent(mockType.getCanonicalName(),
                key -> mock(mockType));
    }
}
 
Example #14
Source Project: hypergraphql   Author: semantic-integration   File: MockitoExtension.java    License: Apache License 2.0 5 votes vote down vote up
private Object getMock(Parameter parameter, ExtensionContext extensionContext) {
    Class<?> mockType = parameter.getType();
    Store mocks = extensionContext.getStore(Namespace.create(MockitoExtension.class, mockType));
    String mockName = getMockName(parameter);

    if (mockName != null) {
        return mocks.getOrComputeIfAbsent(mockName, key -> mock(mockType, mockName));
    }
    else {
        return mocks.getOrComputeIfAbsent(mockType.getCanonicalName(), key -> mock(mockType));
    }
}
 
Example #15
Source Project: database-rider   Author: database-rider   File: DBUnitExtension.java    License: Apache License 2.0 5 votes vote down vote up
private boolean isSpringTestContextEnabled(ExtensionContext extensionContext) {
    if (!extensionContext.getTestClass().isPresent()) {
        return false;
    }
    Store springStore = extensionContext.getRoot().getStore(Namespace.create(SpringExtension.class));
    return springStore != null && springStore.get(extensionContext.getTestClass().get()) != null;
}
 
Example #16
Source Project: database-rider   Author: database-rider   File: DBUnitExtension.java    License: Apache License 2.0 5 votes vote down vote up
private static Optional<io.micronaut.context.ApplicationContext> getMicronautApplicationContext(ExtensionContext extensionContext) {
    Store micronautStore = extensionContext.getRoot().getStore(Namespace.create(MicronautJunit5Extension.class));
    if (micronautStore != null) {
        try {
            io.micronaut.context.ApplicationContext appContext = (io.micronaut.context.ApplicationContext) micronautStore.get(io.micronaut.context.ApplicationContext.class);
            if (appContext != null) {
                return Optional.of(appContext);
            }
        } catch (ClassCastException ex) {
        }
    }
    return Optional.empty();
}
 
Example #17
Source Project: testcontainers-java   Author: testcontainers   File: TestcontainersExtension.java    License: MIT License 5 votes vote down vote up
@Override
public void beforeEach(final ExtensionContext context) {
    Store store = context.getStore(NAMESPACE);

    List<TestLifecycleAware> lifecycleAwareContainers = collectParentTestInstances(context).parallelStream()
        .flatMap(this::findRestartContainers)
        .peek(adapter -> store.getOrComputeIfAbsent(adapter.getKey(), k -> adapter.start()))
        .filter(this::isTestLifecycleAware)
        .map(lifecycleAwareAdapter -> (TestLifecycleAware) lifecycleAwareAdapter.container)
        .collect(toList());

    store.put(LOCAL_LIFECYCLE_AWARE_CONTAINERS, lifecycleAwareContainers);
    signalBeforeTestToContainers(lifecycleAwareContainers, testDescriptionFrom(context));
}
 
Example #18
Source Project: spring-test-junit5   Author: sbrannen   File: SpringExtension.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Get the {@link TestContextManager} associated with the supplied {@code ExtensionContext}.
 * @return the {@code TestContextManager} (never {@code null})
 */
private static TestContextManager getTestContextManager(ExtensionContext context) {
	Assert.notNull(context, "ExtensionContext must not be null");
	Class<?> testClass = context.getRequiredTestClass();
	Store store = getStore(context);
	return store.getOrComputeIfAbsent(testClass, TestContextManager::new, TestContextManager.class);
}
 
Example #19
Source Project: junit-servers   Author: mjeanroy   File: CaptureSystemOutExtension.java    License: MIT License 5 votes vote down vote up
@Override
public void afterEach(ExtensionContext context) throws Exception {
	Store store = getStore(context);

	try {
		closeTemporaryOut(store);
	}
	finally {
		restoreSystemOutAndClearStore(store);
	}
}
 
Example #20
Source Project: junit-servers   Author: mjeanroy   File: CaptureSystemOutExtension.java    License: MIT License 5 votes vote down vote up
/**
 * Override {@code System.out} stream with custom out stream.
 *
 * @param store The extension store.
 */
private static void overrideSystemOut(Store store) {
	ByteArrayOutputStream out = new ByteArrayOutputStream();
	PrintStream outStream = new PrintStream(out);
	System.setOut(outStream);
	store.put(TEMPORARY_OUT_STORE_KEY, out);
}
 
Example #21
Source Project: junit-servers   Author: mjeanroy   File: CaptureSystemOutExtension.java    License: MIT License 5 votes vote down vote up
/**
 * Restore {@code System.out} stream and clear extension store (even if an error
 * occurred while restoring stream).
 *
 * @param store The extension store.
 */
private static void restoreSystemOutAndClearStore(Store store) {
	try {
		restoreSystemOut(store);
	}
	finally {
		clearStore(store);
	}
}
 
Example #22
Source Project: junit-servers   Author: mjeanroy   File: WireMockExtension.java    License: MIT License 5 votes vote down vote up
@Override
public void afterEach(ExtensionContext context) {
	Store store = getStore(context);
	WireMockServer wireMockServer = store.get(STORE_KEY, WireMockServer.class);

	try {
		wireMockServer.stop();
	}
	finally {
		store.remove(STORE_KEY);
	}
}
 
Example #23
/**
 * Retrieves the test data from given dataprovider method.
 *
 * @param dataProviderMethod the dataprovider method that gives the parameters; never {@code null}
 * @param cacheDataProviderResult determines if the dataprovider result should be cached using
 *            {@code dataProviderMethod} as key
 * @param context the execution context to use to create a {@link TestInfo} if required; never {@code null}
 *
 * @return a list of methods, each method bound to a parameter combination returned by the dataprovider
 * @throws NullPointerException if and only if one of the given arguments is {@code null}
 */
protected Object invokeDataProviderMethodToRetrieveData(Method dataProviderMethod, boolean cacheDataProviderResult,
        ExtensionContext context) {
    checkNotNull(dataProviderMethod, "'dataProviderMethod' must not be null");
    checkNotNull(context, "'context' must not be null");

    Store store = context.getRoot().getStore(NAMESPACE_USE_DATAPROVIDER);

    Object cached = store.get(dataProviderMethod);
    if (cached != null) {
        return cached;
    }
    try {
        // TODO how to not require junit-jupiter-engine dependency and reuse already existing ExtensionRegistry?
        ExtensionRegistry extensionRegistry = createRegistryWithDefaultExtensions(
                new DefaultJupiterConfiguration(emptyConfigurationParameters()));
        Object data = executableInvoker.invoke(dataProviderMethod, context.getTestInstance().orElse(null), context,
                extensionRegistry, InvocationInterceptor::interceptTestFactoryMethod);
        if (cacheDataProviderResult) {
            store.put(dataProviderMethod, data);
        }
        return data;

    } catch (Exception e) {
        throw new ParameterResolutionException(
                String.format("Exception while invoking dataprovider method '%s': %s", dataProviderMethod.getName(),
                        e.getMessage()),
                e);
    }
}
 
Example #24
/**
 * Retrieves the test data from given dataprovider method.
 *
 * @param dataProviderMethod the dataprovider method that gives the parameters; never {@code null}
 * @param cacheDataProviderResult determines if the dataprovider result should be cached using
 *            {@code dataProviderMethod} as key
 * @param context the execution context to use to create a {@link TestInfo} if required; never {@code null}
 *
 * @return a list of methods, each method bound to a parameter combination returned by the dataprovider
 * @throws NullPointerException if and only if one of the given arguments is {@code null}
 */
protected Object invokeDataProviderMethodToRetrieveData(Method dataProviderMethod, boolean cacheDataProviderResult,
        ExtensionContext context) {
    checkNotNull(dataProviderMethod, "'dataProviderMethod' must not be null");
    checkNotNull(context, "'context' must not be null");

    Store store = context.getRoot().getStore(NAMESPACE_USE_DATAPROVIDER);

    Object cached = store.get(dataProviderMethod);
    if (cached != null) {
        return cached;
    }
    try {
        // TODO how to not require junit-jupiter-engine dependency and reuse already existing ExtensionRegistry?
        ExtensionRegistry extensionRegistry = createRegistryWithDefaultExtensions(
                new DefaultJupiterConfiguration(emptyConfigurationParameters()));
        Object data = executableInvoker.invoke(dataProviderMethod, context.getTestInstance().orElse(null), context,
                extensionRegistry, InvocationInterceptor::interceptTestFactoryMethod);
        if (cacheDataProviderResult) {
            store.put(dataProviderMethod, data);
        }
        return data;

    } catch (Exception e) {
        throw new ParameterResolutionException(
                String.format("Exception while invoking dataprovider method '%s': %s", dataProviderMethod.getName(),
                        e.getMessage()),
                e);
    }
}
 
Example #25
Source Project: spring-analysis-note   Author: Vip-Augus   File: SpringExtension.java    License: MIT License 4 votes vote down vote up
private static Store getStore(ExtensionContext context) {
	return context.getRoot().getStore(NAMESPACE);
}
 
Example #26
Source Project: java-technology-stack   Author: codeEngraver   File: SpringExtension.java    License: MIT License 4 votes vote down vote up
private static Store getStore(ExtensionContext context) {
	return context.getRoot().getStore(NAMESPACE);
}
 
Example #27
Source Project: judgels   Author: ia-toki   File: HibernateSessionExtension.java    License: GNU General Public License v2.0 4 votes vote down vote up
private Store getStore(ExtensionContext context) {
    return context.getStore(Namespace.create(getClass(), context));
}
 
Example #28
Source Project: mastering-junit5   Author: bonigarcia   File: TimingExtension.java    License: Apache License 2.0 4 votes vote down vote up
private Store getStore(ExtensionContext context) {
    return context.getStore(Namespace.create(getClass(), context));
}
 
Example #29
Source Project: taskana   Author: Taskana   File: JaasExtension.java    License: 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());
}
 
Example #30
Source Project: Mastering-Software-Testing-with-JUnit-5   Author: PacktPublishing   File: TimingExtension.java    License: MIT License 4 votes vote down vote up
private Store getStore(ExtensionContext context) {
    return context.getStore(Namespace.create(getClass(), context));
}