Skip to content

Commit

Permalink
Fix for #802 Removed thrown IllegalArgumentException to make sure non…
Browse files Browse the repository at this point in the history
…-spock-tests … (#803)

* Removed thrown IllegalArgumentException to make sure non-spock-tests executed by SpringRunner don't fail
  • Loading branch information
Andreas Neumann authored and leonard84 committed Jan 9, 2018
1 parent 01460ba commit 2cc4107
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.spockframework.spring5;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
* Make sure we still can correctly execute tests executed by the SpringRunner that are not spock tests
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class SpringRunnerTest {

@Test
public void testThatSpringRunnerExecutesCorrectly() {
Assert.assertTrue(true);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,46 +46,45 @@ public void beforeTestClass(SpringTestContext testContext) throws Exception {

@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(testContext.getTestInstance());
mockPostprocessor.injectSpies(testInstance);
}
}

@Override
public void beforeTestMethod(SpringTestContext testContext) throws Exception {
Object testInstance = testContext.getTestInstance();
if (!(testInstance instanceof Specification)) return;

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<>(Arrays.asList(scanScopedBeans.value()));
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<>();
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()){
continue;
}
if (beanDefinition.isSingleton() || scanScopedBean(scanScopedBeans, scopes, beanDefinition)){
Object bean = applicationContext.getBean(beanName);
if (mockUtil.isMock(bean)) {
mockUtil.attachMock(bean, specification);
mockedBeans.add(bean);
}
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.");
}

testContext.setAttribute(MOCKED_BEANS_LIST, mockedBeans);
}

private boolean scanScopedBean(ScanScopedBeans scanScopedBeans, Set<String> scopes, BeanDefinition beanDefinition) {
Expand Down

0 comments on commit 2cc4107

Please sign in to comment.