下面列出了怎么用org.junit.jupiter.api.extension.RegisterExtension的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void beforeEach(ExtensionContext context) throws Exception {
if (assertException != null) {
// Build failed as expected - test methods are not invoked
return;
}
if (runningQuarkusApplication != null) {
runningQuarkusApplication.getClassLoader().loadClass(RestAssuredURLManager.class.getName())
.getDeclaredMethod("setURL", boolean.class).invoke(null, useSecureConnection);
} else {
Optional<Class<?>> testClass = context.getTestClass();
if (testClass.isPresent()) {
Field extensionField = Arrays.stream(testClass.get().getDeclaredFields()).filter(
f -> f.isAnnotationPresent(RegisterExtension.class) && QuarkusUnitTest.class.equals(f.getType()))
.findAny().orElse(null);
if (extensionField != null && !Modifier.isStatic(extensionField.getModifiers())) {
throw new IllegalStateException(
"Test application not started - QuarkusUnitTest must be used with a static field: "
+ extensionField);
}
}
throw new IllegalStateException("Test application not started for an unknown reason");
}
}
@Override
protected DropwizardTestSupport<?> prepareTestSupport(final ExtensionContext context) {
if (config == null) {
// Configure from annotation
// Note that it is impossible to have both manually build config and annotation because annotation
// will be processed first and manual registration will be simply ignored
final TestGuiceyApp ann = AnnotationSupport
// also search annotation inside other annotations (meta)
.findAnnotation(context.getElement(), TestGuiceyApp.class).orElse(null);
// catch incorrect usage by direct @ExtendWith(...)
Preconditions.checkNotNull(ann, "%s annotation not declared: can't work without configuration, "
+ "so either use annotation or extension with @%s for manual configuration",
TestGuiceyApp.class.getSimpleName(),
RegisterExtension.class.getSimpleName());
config = Config.parse(ann);
}
HooksUtil.register(config.hooks);
// config overrides work through system properties so it is important to have unique prefixes
final String configPrefix = ConfigOverrideUtils.createPrefix(context.getRequiredTestClass());
return create(context, config.app, config.configPath, configPrefix, config.configOverrides);
}
@Override
@SuppressWarnings("unchecked")
protected DropwizardTestSupport<?> prepareTestSupport(final ExtensionContext context) {
if (config == null) {
// Configure from annotation
// Note that it is impossible to have both manually build config and annotation because annotation
// will be processed first and manual registration will be simply ignored
final TestDropwizardApp ann = AnnotationSupport
// also search annotation inside other annotations (meta)
.findAnnotation(context.getElement(), TestDropwizardApp.class).orElse(null);
// catch incorrect usage by direct @ExtendWith(...)
Preconditions.checkNotNull(ann, "%s annotation not declared: can't work without configuration, "
+ "so either use annotation or extension with @%s for manual configuration",
TestDropwizardApp.class.getSimpleName(),
RegisterExtension.class.getSimpleName());
config = Config.parse(ann);
}
HooksUtil.register(config.hooks);
// config overrides work through system properties so it is important to have unique prefixes
final String configPrefix = ConfigOverrideUtils.createPrefix(context.getRequiredTestClass());
final DropwizardTestSupport support = new DropwizardTestSupport(config.app,
config.configPath,
configPrefix,
buildConfigOverrides(configPrefix));
if (config.randomPorts) {
support.addListener(new RandomPortsListener());
}
return support;
}