Search in sources :

Example 1 with MockBean

use of io.micronaut.test.annotation.MockBean in project micronaut-test by micronaut-projects.

the class TestActiveCondition method matches.

@Override
public boolean matches(ConditionContext context) {
    if (context.getComponent() instanceof BeanDefinition) {
        BeanDefinition<?> definition = (BeanDefinition<?>) context.getComponent();
        final BeanContext beanContext = context.getBeanContext();
        final Optional<Class<?>> declaringType = definition.getDeclaringType();
        if (beanContext instanceof ApplicationContext) {
            ApplicationContext applicationContext = (ApplicationContext) beanContext;
            final Class activeSpecClazz = applicationContext.get(ACTIVE_SPEC_CLAZZ, Class.class).orElse(null);
            final String activeSpecName = Optional.ofNullable(activeSpecClazz).map(clazz -> clazz.getPackage().getName() + "." + clazz.getSimpleName()).orElse(null);
            if (definition.isAnnotationPresent(MockBean.class) && declaringType.isPresent()) {
                final Class<?> declaringTypeClass = declaringType.get();
                String declaringTypeName = declaringTypeClass.getName();
                if (activeSpecClazz != null) {
                    final String packageName = NameUtils.getPackageName(activeSpecName);
                    final String simpleName = NameUtils.getSimpleName(activeSpecName);
                    final String rootName = packageName + ".$" + simpleName;
                    if (definition.isProxy()) {
                        return declaringTypeClass.isAssignableFrom(activeSpecClazz) || declaringTypeName.equals(rootName) || declaringTypeName.startsWith(rootName + "$");
                    } else {
                        return declaringTypeClass.isAssignableFrom(activeSpecClazz) && // Check if super class def is part of the current spec
                        definition.getClass().getName().startsWith(rootName + "$") || activeSpecName.equals(declaringTypeName) || declaringTypeName.startsWith(activeSpecName + "$");
                    }
                } else {
                    context.fail("@MockBean of type " + definition.getBeanType() + " not within scope of parent test.");
                    return false;
                }
            } else {
                if (activeSpecName != null) {
                    boolean beanTypeMatches = activeSpecName.equals(definition.getBeanType().getName());
                    return beanTypeMatches || (declaringType.isPresent() && activeSpecClazz == declaringType.get());
                } else {
                    return false;
                }
            }
        } else {
            return false;
        }
    } else {
        return true;
    }
}
Also used : ApplicationContext(io.micronaut.context.ApplicationContext) MockBean(io.micronaut.test.annotation.MockBean) Condition(io.micronaut.context.condition.Condition) BeanContext(io.micronaut.context.BeanContext) Optional(java.util.Optional) BeanDefinition(io.micronaut.inject.BeanDefinition) NameUtils(io.micronaut.core.naming.NameUtils) ConditionContext(io.micronaut.context.condition.ConditionContext) BeanContext(io.micronaut.context.BeanContext) ApplicationContext(io.micronaut.context.ApplicationContext) MockBean(io.micronaut.test.annotation.MockBean) BeanDefinition(io.micronaut.inject.BeanDefinition)

Aggregations

ApplicationContext (io.micronaut.context.ApplicationContext)1 BeanContext (io.micronaut.context.BeanContext)1 Condition (io.micronaut.context.condition.Condition)1 ConditionContext (io.micronaut.context.condition.ConditionContext)1 NameUtils (io.micronaut.core.naming.NameUtils)1 BeanDefinition (io.micronaut.inject.BeanDefinition)1 MockBean (io.micronaut.test.annotation.MockBean)1 Optional (java.util.Optional)1