forked from spockframework/spock
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wrap Spring's TestContext to support Spring 2.5 and 3.
(cherry picked from commit 28b0156)
- Loading branch information
Showing
9 changed files
with
168 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
|
14 changes: 14 additions & 0 deletions
14
...g/spring2-test/src/test/groovy/org/spockframework/spring2/RuntimeCompatibilitySpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
...2-test/src/test/resources/org/spockframework/spring2/RuntimeCompatibilitySpec-context.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
|
15 changes: 15 additions & 0 deletions
15
...g/spring3-test/src/test/groovy/org/spockframework/spring3/RuntimeCompatibilitySpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
...k-spring/src/main/java/org/spockframework/spring/AbstractSpringTestExecutionListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
spock-spring/src/main/java/org/spockframework/spring/SpringTestContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |