Search in sources :

Example 1 with TypedMock

use of org.apache.deltaspike.testcontrol.api.mock.TypedMock in project deltaspike by apache.

the class AbstractMockManager method addMock.

@Override
public void addMock(Object mockInstance, Annotation... qualifiers) {
    //check if this method gets used without changing the default-config
    if (!TestBaseConfig.MockIntegration.ALLOW_MOCKED_BEANS && !TestBaseConfig.MockIntegration.ALLOW_MOCKED_PRODUCERS) {
        throw new IllegalStateException("The support for mocked CDI-Beans is disabled " + "due to a reduced portability across different CDI-implementations. " + "Please set '" + TestBaseConfig.MockIntegration.ALLOW_MOCKED_BEANS_KEY + "' and/or '" + TestBaseConfig.MockIntegration.ALLOW_MOCKED_PRODUCERS_KEY + "' to 'true' " + "(in 'META-INF/apache-deltaspike.properties') on your test-classpath.");
    }
    Class<?> mockClass = mockInstance.getClass();
    Class<?> beanClass = mockClass.getSuperclass();
    if (beanClass == null) {
        beanClass = mockClass;
    }
    if (Object.class.equals(beanClass)) {
        throw new IllegalArgumentException(mockInstance.getClass().getName() + " isn't a supported approach for mocking -> please extend from the original class.");
    }
    TypedMock typedMock = mockClass.getAnnotation(TypedMock.class);
    if (typedMock == null) {
        typedMock = beanClass.getAnnotation(TypedMock.class);
    }
    Class[] specifiedTypes = null;
    if (typedMock != null) {
        specifiedTypes = typedMock.value();
    } else {
        Typed typed = mockClass.getAnnotation(Typed.class);
        if (typed == null || typed.value().length == 0) {
            typed = beanClass.getAnnotation(Typed.class);
        }
        if (typed != null && typed.value().length > 0) {
            specifiedTypes = typed.value();
        }
    }
    if (specifiedTypes != null) {
        for (Class typedClass : specifiedTypes) {
            this.registeredMocks.put(new BeanCacheKey(typedClass, qualifiers), mockInstance);
        }
    } else {
        this.registeredMocks.put(new BeanCacheKey(beanClass, qualifiers), mockInstance);
    }
}
Also used : Typed(javax.enterprise.inject.Typed) TypedMock(org.apache.deltaspike.testcontrol.api.mock.TypedMock)

Aggregations

Typed (javax.enterprise.inject.Typed)1 TypedMock (org.apache.deltaspike.testcontrol.api.mock.TypedMock)1