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

The following examples show how to use org.junit.jupiter.api.extension.RegisterExtension. 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: QuarkusUnitTest.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@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");
    }
}
 
Example #2
Source File: TestGuiceyAppExtension.java    From dropwizard-guicey with MIT License 6 votes vote down vote up
@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);
}
 
Example #3
Source File: TestDropwizardAppExtension.java    From dropwizard-guicey with MIT License 5 votes vote down vote up
@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;
}