下面列出了org.junit.jupiter.api.extension.ExtensionConfigurationException#org.testcontainers.junit.jupiter.Container 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Set<GenericContainer<?>> discoverContainers(Class<?> clazz) {
Set<GenericContainer<?>> discoveredContainers = new HashSet<>();
for (Field containerField : AnnotationSupport.findAnnotatedFields(clazz, Container.class)) {
if (!Modifier.isPublic(containerField.getModifiers()))
throw new ExtensionConfigurationException("@Container annotated fields must be public visibility");
if (!Modifier.isStatic(containerField.getModifiers()))
throw new ExtensionConfigurationException("@Container annotated fields must be static");
boolean isStartable = GenericContainer.class.isAssignableFrom(containerField.getType());
if (!isStartable)
throw new ExtensionConfigurationException("@Container annotated fields must be a subclass of " + GenericContainer.class);
try {
GenericContainer<?> startableContainer = (GenericContainer<?>) containerField.get(null);
discoveredContainers.add(startableContainer);
} catch (IllegalArgumentException | IllegalAccessException e) {
LOG.warn("Unable to access field " + containerField, e);
}
}
return discoveredContainers;
}
private Set<GenericContainer<?>> discoverContainers(Class<?> clazz) {
Set<GenericContainer<?>> discoveredContainers = new HashSet<>();
for (Field containerField : AnnotationSupport.findAnnotatedFields(clazz, Container.class)) {
if (!Modifier.isPublic(containerField.getModifiers()))
throw new ExtensionConfigurationException("@Container annotated fields must be public visibility");
if (!Modifier.isStatic(containerField.getModifiers()))
throw new ExtensionConfigurationException("@Container annotated fields must be static");
boolean isStartable = GenericContainer.class.isAssignableFrom(containerField.getType());
if (!isStartable)
throw new ExtensionConfigurationException("@Container annotated fields must be a subclass of " + GenericContainer.class);
try {
GenericContainer<?> startableContainer = (GenericContainer<?>) containerField.get(null);
discoveredContainers.add(startableContainer);
} catch (IllegalArgumentException | IllegalAccessException e) {
LOG.warn("Unable to access field " + containerField, e);
}
}
return discoveredContainers;
}
private MicroProfileApplication<?> autoDiscoverMPApp(Class<?> clazz, boolean errorIfNone) {
// First check for any MicroProfileApplicaiton directly present on the test class
List<Field> mpApps = AnnotationSupport.findAnnotatedFields(clazz, Container.class,
f -> Modifier.isStatic(f.getModifiers()) &&
Modifier.isPublic(f.getModifiers()) &&
MicroProfileApplication.class.isAssignableFrom(f.getType()),
HierarchyTraversalMode.TOP_DOWN);
if (mpApps.size() == 1)
try {
return (MicroProfileApplication<?>) mpApps.get(0).get(null);
} catch (IllegalArgumentException | IllegalAccessException e) {
// This should never happen because we only look for fields that are public+static
e.printStackTrace();
}
if (mpApps.size() > 1)
throw new ExtensionConfigurationException("Should be no more than 1 public static MicroProfileApplication field on " + clazz);
// If none found, check any SharedContainerConfig
String sharedConfigMsg = "";
if (sharedConfigClass != null) {
MicroProfileApplication<?> mpApp = autoDiscoverMPApp(sharedConfigClass, false);
if (mpApp != null)
return mpApp;
sharedConfigMsg = " or " + sharedConfigClass;
}
if (errorIfNone)
throw new ExtensionConfigurationException("No public static MicroProfileApplication fields annotated with @Container were located " +
"on " + clazz + sharedConfigMsg + " to auto-connect with REST-client fields.");
return null;
}