Search in sources :

Example 1 with Extension

use of org.junit.jupiter.api.extension.Extension in project junit5 by junit-team.

the class ExtensionUtils method registerExtensionsFromFields.

/**
 * Register extensions in the supplied registry from fields in the supplied
 * class that are annotated with {@link RegisterExtension @RegisterExtension}.
 *
 * @param registry the registry in which to register the extensions; never {@code null}
 * @param clazz the class or interface in which to find the fields; never {@code null}
 * @param instance the instance of the supplied class; may be {@code null}
 * when searching for {@code static} fields in the class
 */
static void registerExtensionsFromFields(ExtensionRegistry registry, Class<?> clazz, Object instance) {
    Preconditions.notNull(clazz, "Class must not be null");
    Preconditions.notNull(registry, "ExtensionRegistry must not be null");
    Predicate<Field> predicate = (instance == null) ? isStaticExtension : isNonStaticExtension;
    findAnnotatedFields(clazz, RegisterExtension.class, predicate).forEach(field -> {
        readFieldValue(field, instance).ifPresent(value -> {
            Extension extension = (Extension) value;
            registry.registerExtension(extension, field);
        });
    });
}
Also used : Extension(org.junit.jupiter.api.extension.Extension) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Field(java.lang.reflect.Field) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension)

Aggregations

Field (java.lang.reflect.Field)1 Extension (org.junit.jupiter.api.extension.Extension)1 RegisterExtension (org.junit.jupiter.api.extension.RegisterExtension)1