下面列出了怎么用org.junit.jupiter.api.extension.ExtensionContext.Namespace的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
void testInvokeDataProviderMethodToRetrieveDataShouldThrowParameterResolutionExceptionIfDataProviderInvocationThrows()
throws Exception {
// Given:
Method dataProviderMethod = this.getClass().getDeclaredMethod(
"testInvokeDataProviderMethodToRetrieveDataShouldThrowParameterResolutionExceptionIfDataProviderInvocationThrows");
when(extensionContext.getRoot()).thenReturn(extensionContext);
when(extensionContext.getStore(any(Namespace.class))).thenReturn(store);
// When:
Exception result = assertThrows(ParameterResolutionException.class,
() -> underTest.invokeDataProviderMethodToRetrieveData(dataProviderMethod, true, extensionContext));
// Then:
assertThat(result).hasMessageMatching("Exception while invoking dataprovider method '.*': .*");
}
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));
}
}
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));
}
}
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));
}
}
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));
}
}
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));
}
}
private boolean isSpringExtensionEnabled(ExtensionContext extensionContext) {
try {
return isOnClasspath("org.springframework.test.context.junit.jupiter.SpringExtension") && extensionContext.getRoot().getStore(Namespace.create(SpringExtension.class)) != null;
} catch (Exception e) {
return false;
}
}
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;
}
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();
}
/**
* Retrieves injector from the context store using Namespace. If it is not present it searches in
* parent context until it is found or there is end of hierarchy.
*
* @param context - extension context from junit5 extensions
* @param namespace - Namespace for current test invocation
* @return Injector or null if no injector is found
*/
public static Injector retrieveInjectorFromStore(ExtensionContext context, Namespace namespace) {
AnnotatedElement element = context.getElement()
.orElseThrow(() -> new NoSuchElementException("No element present"));
return context.getStore(namespace).getOrComputeIfAbsent(element,
absent -> retrieveInjectorFromStore(
context.getParent().orElseThrow(() -> new NoSuchElementException("No injector found")),
namespace), Injector.class);
}
private Store getStore(ExtensionContext context) {
return context.getStore(Namespace.create(getClass(), context));
}
private Store getStore(ExtensionContext context) {
return context.getStore(Namespace.create(getClass(), context));
}
private Store getStore(ExtensionContext context) {
return context.getStore(Namespace.create(getClass(), context));
}
private Store store(ExtensionContext extensionContext) {
return extensionContext.getStore(Namespace.GLOBAL);
}
private Store getStore(ExtensionContext context) {
return context.getRoot().getStore(Namespace.create(getClass()));
}
private Store getStore(ExtensionContext context) {
return context.getStore(Namespace.create(getClass(), context.getRequiredTestMethod()));
}
private Store getStore(ExtensionContext context) {
return context.getStore(Namespace.create(context.getRequiredTestMethod()));
}
private Namespace localNamespace(ExtensionContext context) {
return Namespace.create(TempDirectoryExtension.class, context);
}
private Store getStore(ExtensionContext context) {
Namespace namespace = Namespace.create(getClass(), context.getRequiredTestMethod());
return context.getStore(namespace);
}
public ExtensionContextParamResolver(Namespace namespace) {
this(namespace, new EmptyResolver());
}
private ExtensionContextParamResolver(Namespace namespace, ParameterResolver parent) {
this.namespace = namespace;
this.parent = parent;
}
/**
* We use custom namespace based on this extension class; can be stored as static variable as it doesn't change throughout
* testsuite execution.
*
* @param context {@link ExtensionContext} you are currently using
* @return <b>Root</b> {@link ExtensionContext.Store} with {@link Namespace} based on extension class alone
*/
private static synchronized ExtensionContext.Store getRootExtensionStore(ExtensionContext context) {
if (EXTENSION_NAMESPACE == null) {
EXTENSION_NAMESPACE = Namespace.create(WeldJunit5Extension.class);
}
return context.getRoot().getStore(EXTENSION_NAMESPACE);
}
/**
* Gets the store with a <b>method-level</b> scope.
*
* @param context context for current extension
* @return The store
*/
private Store getStore(ExtensionContext context) {
return context.getStore(Namespace.create(getClass(), context.getRequiredTestMethod()));
}
/**
* We use custom namespace based on this extension class and test class, cannot be stored as static variable as test class
* name changes throughout testsuite execution
*
* @param context {@link ExtensionContext} you are currently using
* @return {@link ExtensionContext.Store} based on {@link ExtensionContext} and the required test class
*/
private static ExtensionContext.Store getTestStore(ExtensionContext context) {
return context.getStore(Namespace.create(WeldJunit5Extension.class, context.getRequiredTestClass()));
}