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.");
}
}
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);
}
}
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);
}
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);
}
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;
}
Aggregations