Search in sources :

Example 1 with Specification

use of spock.lang.Specification in project spock by spockframework.

the class SpringMockTestExecutionListener method beforeTestMethod.

public void beforeTestMethod(TestContext testContext) throws Exception {
    Object testInstance = testContext.getTestInstance();
    if (testInstance instanceof Specification) {
        Specification specification = (Specification) testInstance;
        ScanScopedBeans scanScopedBeans = ReflectionUtil.getAnnotationRecursive(specification.getClass(), ScanScopedBeans.class);
        Set<String> scopes = scanScopedBeans == null ? Collections.<String>emptySet() : new HashSet<String>(Arrays.asList(scanScopedBeans.value()));
        ApplicationContext applicationContext = testContext.getApplicationContext();
        String[] mockBeanNames = applicationContext.getBeanDefinitionNames();
        List<Object> mockedBeans = new ArrayList<Object>();
        for (String beanName : mockBeanNames) {
            BeanDefinition beanDefinition = ((BeanDefinitionRegistry) applicationContext).getBeanDefinition(beanName);
            if (beanDefinition.isAbstract()) {
                continue;
            }
            if (beanDefinition.isSingleton() || scanScopedBean(scanScopedBeans, scopes, beanDefinition)) {
                Object bean = applicationContext.getBean(beanName);
                if (mockUtil.isMock(bean)) {
                    mockUtil.attachMock(bean, specification);
                    mockedBeans.add(bean);
                }
            }
        }
        testContext.setAttribute(MOCKED_BEANS_LIST, mockedBeans);
    } else {
        throw new IllegalArgumentException("SpringMockTestExecutionListener is only applicable for spock specifications.");
    }
}
Also used : BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) Specification(spock.lang.Specification) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ApplicationContext(org.springframework.context.ApplicationContext)

Example 2 with Specification

use of spock.lang.Specification in project spock by spockframework.

the class SpringMockTestExecutionListener method prepareTestInstance.

@Override
public void prepareTestInstance(SpringTestContext testContext) throws Exception {
    Object testInstance = testContext.getTestInstance();
    if (!(testInstance instanceof Specification))
        return;
    ApplicationContext applicationContext = testContext.getApplicationContext();
    if (applicationContext.containsBean(SpockMockPostprocessor.class.getName())) {
        SpockMockPostprocessor mockPostprocessor = applicationContext.getBean(SpockMockPostprocessor.class);
        mockPostprocessor.injectSpies(testInstance);
    }
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) Specification(spock.lang.Specification) SpockMockPostprocessor(org.spockframework.spring.mock.SpockMockPostprocessor)

Example 3 with Specification

use of spock.lang.Specification in project spock by spockframework.

the class EmptyOrDummyResponse method createDummy.

private Object createDummy(IMockInvocation invocation) {
    Class<?> type = invocation.getMethod().getReturnType();
    Type genericType = invocation.getMethod().getExactReturnType();
    Specification spec = invocation.getMockObject().getSpecification();
    return spec.createMock("dummy", genericType, MockNature.STUB, GroovyObject.class.isAssignableFrom(type) ? MockImplementation.GROOVY : MockImplementation.JAVA, Collections.emptyMap(), null);
}
Also used : Specification(spock.lang.Specification)

Example 4 with Specification

use of spock.lang.Specification in project spock by spockframework.

the class SpringMockTestExecutionListener method beforeTestMethod.

@Override
public void beforeTestMethod(SpringTestContext testContext) throws Exception {
    Object testInstance = testContext.getTestInstance();
    if (!(testInstance instanceof Specification))
        return;
    Specification specification = (Specification) testInstance;
    ScanScopedBeans scanScopedBeans = ReflectionUtil.getAnnotationRecursive(specification.getClass(), ScanScopedBeans.class);
    Set<String> scopes = scanScopedBeans == null ? Collections.<String>emptySet() : new HashSet<>(Arrays.asList(scanScopedBeans.value()));
    ApplicationContext applicationContext = testContext.getApplicationContext();
    String[] mockBeanNames = applicationContext.getBeanDefinitionNames();
    List<Object> mockedBeans = new ArrayList<>();
    for (String beanName : mockBeanNames) {
        BeanDefinition beanDefinition = ((BeanDefinitionRegistry) applicationContext).getBeanDefinition(beanName);
        if (beanDefinition.isAbstract() || beanDefinition.isLazyInit()) {
            continue;
        }
        if (beanDefinition.isSingleton() || scanScopedBean(scanScopedBeans, scopes, beanDefinition)) {
            Object bean = applicationContext.getBean(beanName);
            if (mockUtil.isMock(bean)) {
                mockUtil.attachMock(bean, specification);
                mockedBeans.add(bean);
            }
        }
    }
    testContext.setAttribute(MOCKED_BEANS_LIST, mockedBeans);
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) Specification(spock.lang.Specification) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 5 with Specification

use of spock.lang.Specification in project spock by spockframework.

the class PlatformSpecRunner method createSpecInstance.

SpockExecutionContext createSpecInstance(SpockExecutionContext context, boolean shared) {
    if (context.getErrorInfoCollector().hasErrors())
        return context;
    Specification instance;
    try {
        instance = (Specification) context.getSpec().getReflection().newInstance();
    } catch (Throwable t) {
        throw new InternalSpockError("Failed to instantiate spec '%s'", t).withArgs(context.getSpec().getName());
    }
    context = context.withCurrentInstance(instance);
    getSpecificationContext(context).setCurrentSpec(context.getSpec());
    if (shared) {
        context = context.withSharedInstance(instance);
    }
    getSpecificationContext(context).setSharedInstance(context.getSharedInstance());
    return context;
}
Also used : Specification(spock.lang.Specification)

Aggregations

Specification (spock.lang.Specification)5 ApplicationContext (org.springframework.context.ApplicationContext)3 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)2 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)2 SpockMockPostprocessor (org.spockframework.spring.mock.SpockMockPostprocessor)1