Skip to content

Commit

Permalink
Wrap Spring's TestContext to support Spring 2.5 and 3.
Browse files Browse the repository at this point in the history
(cherry picked from commit 28b0156)
  • Loading branch information
egrajeda authored and leonard84 committed Mar 23, 2017
1 parent 21f5eb7 commit 979de6d
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 8 deletions.
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ include "spock-bom"
include "spock-core"
include "spock-specs"
include "spock-spring"
include "spock-spring:spring2-test"
include "spock-spring:spring3-test"
include "spock-guice"
include "spock-tapestry"
include "spock-unitils"
Expand Down
12 changes: 12 additions & 0 deletions spock-spring/spring2-test/spring2-test.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def springVersion = "2.5.6.SEC03"

dependencies {
compile "org.springframework:spring-core:$springVersion"

testCompile project(":spock-core")
testCompile "org.springframework:spring-context:$springVersion"
testCompile ("org.springframework:spring-test:$springVersion") { force = true }

testRuntime project(":spock-spring")
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.spockframework.spring2

import org.springframework.test.context.ContextConfiguration
import spock.lang.Specification

@ContextConfiguration
class RuntimeCompatibilitySpec extends Specification {

def "no runtime errors are thrown"() {
expect:
1 == 1
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
</beans>
12 changes: 12 additions & 0 deletions spock-spring/spring3-test/spring3-test.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def springVersion = "3.2.16.RELEASE"

dependencies {
compile "org.springframework:spring-core:$springVersion"

testCompile project(":spock-core")
testCompile "org.springframework:spring-context:$springVersion"
testCompile ("org.springframework:spring-test:$springVersion") { force = true }

testRuntime project(":spock-spring")
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.spockframework.spring3

import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.support.AnnotationConfigContextLoader
import spock.lang.Specification

@ContextConfiguration(loader = AnnotationConfigContextLoader)
class RuntimeCompatibilitySpec extends Specification {

def "no runtime errors are thrown"() {
expect:
1 == 1
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.spockframework.spring;

import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestExecutionListener;

public abstract class AbstractSpringTestExecutionListener implements TestExecutionListener {
@Override
public void beforeTestClass(TestContext testContext) throws Exception {
beforeTestClass(new SpringTestContext(testContext));
}

public abstract void beforeTestClass(SpringTestContext testContext) throws Exception;

@Override
public void prepareTestInstance(TestContext testContext) throws Exception {
prepareTestInstance(new SpringTestContext(testContext));
}

public abstract void prepareTestInstance(SpringTestContext testContext) throws Exception;

@Override
public void beforeTestMethod(TestContext testContext) throws Exception {
beforeTestMethod(new SpringTestContext(testContext));
}

public abstract void beforeTestMethod(SpringTestContext testContext) throws Exception;

@Override
public void afterTestMethod(TestContext testContext) throws Exception {
afterTestMethod(new SpringTestContext(testContext));
}

public abstract void afterTestMethod(SpringTestContext testContext) throws Exception;

@Override
public void afterTestClass(TestContext testContext) throws Exception {
afterTestClass(new SpringTestContext(testContext));
}

public abstract void afterTestClass(SpringTestContext testContext) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestExecutionListener;
import spock.lang.Specification;

Expand All @@ -17,19 +16,22 @@
*
* @author Leonard Bruenings
*/
public class SpringMockTestExecutionListener implements TestExecutionListener {
public class SpringMockTestExecutionListener extends AbstractSpringTestExecutionListener {

public static final String MOCKED_BEANS_LIST = "org.spockframework.spring.SpringMockTestExecutionListener.MOCKED_BEANS_LIST";

private final MockUtil mockUtil = new MockUtil();

public void beforeTestClass(TestContext testContext) throws Exception {
@Override
public void beforeTestClass(SpringTestContext testContext) throws Exception {
}

public void prepareTestInstance(TestContext testContext) throws Exception {
@Override
public void prepareTestInstance(SpringTestContext testContext) throws Exception {
}

public void beforeTestMethod(TestContext testContext) throws Exception {
@Override
public void beforeTestMethod(SpringTestContext testContext) throws Exception {
Object testInstance = testContext.getTestInstance();

if (testInstance instanceof Specification) {
Expand Down Expand Up @@ -68,8 +70,9 @@ private boolean scanScopedBean(ScanScopedBeans scanScopedBeans, Set<String> scop
return scanScopedBeans != null && (scopes.size() == 0 || scopes.contains(beanDefinition.getScope()));
}

@Override
@SuppressWarnings("unchecked")
public void afterTestMethod(TestContext testContext) throws Exception {
public void afterTestMethod(SpringTestContext testContext) throws Exception {
List<Object> mockedBeans = (List<Object>) testContext.getAttribute(MOCKED_BEANS_LIST);

if (mockedBeans != null) {
Expand All @@ -79,7 +82,7 @@ public void afterTestMethod(TestContext testContext) throws Exception {
}
}

public void afterTestClass(TestContext testContext) throws Exception {
@Override
public void afterTestClass(SpringTestContext testContext) throws Exception {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.spockframework.spring;

import org.spockframework.util.ReflectionUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.TestContext;

import java.lang.reflect.Method;

/**
* Wrapper around Spring's TestContext class that works with Spring 2.5 and Spring 3.
*/
public class SpringTestContext {
private static final Method getApplicationContextMethod =
ReflectionUtil.getMethodBySignature(TestContext.class, "getApplicationContext");
private static final Method getTestInstanceMethod =
ReflectionUtil.getMethodBySignature(TestContext.class, "getTestInstance");
private static final Method setAttributeMethod =
ReflectionUtil.getMethodBySignature(TestContext.class, "setAttribute", String.class, Object.class);
private static final Method getAttributeMethod =
ReflectionUtil.getMethodBySignature(TestContext.class, "getAttribute", String.class);

private final TestContext delegate;

public SpringTestContext(TestContext testContext) {
delegate = testContext;
}

public ApplicationContext getApplicationContext() {
if (getApplicationContextMethod == null) {
throw new SpringExtensionException("Method 'TestContext.getApplicationContext()' was not found");
}
return (ApplicationContext) ReflectionUtil.invokeMethod(delegate, getApplicationContextMethod);
}

public Object getTestInstance() {
if (getTestInstanceMethod == null) {
throw new SpringExtensionException("Method 'TestContext.getTestInstance()' was not found");
}
return ReflectionUtil.invokeMethod(delegate, getTestInstanceMethod);
}

public void setAttribute(String name, Object value) {
if (setAttributeMethod == null) {
throw new SpringExtensionException("Method 'TestContext.setAttribute()' was not found");
}
ReflectionUtil.invokeMethod(delegate, setAttributeMethod, name, value);
}

public Object getAttribute(String name) {
if (getAttributeMethod == null) {
throw new SpringExtensionException("Method 'TestContext.getAttribute()' was not found");
}
return ReflectionUtil.invokeMethod(delegate, getAttributeMethod, name);
}
}

0 comments on commit 979de6d

Please sign in to comment.