Skip to content

Commit 69f283a

Browse files
committed
Fixes problems with Spock and @Rollback - Fixes #9493
1 parent 3e33dcd commit 69f283a

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

grails-core/src/main/groovy/org/grails/transaction/transform/TransactionalTransform.groovy

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ class TransactionalTransform implements ASTTransformation{
119119

120120
if( methodName.contains('$') && !methodName.startsWith('$spock') ) continue
121121

122+
if(md.getAnnotations().any { AnnotationNode an -> an.classNode.name == "org.spockframework.runtime.model.DataProviderMetadata"}) {
123+
continue
124+
}
125+
122126
if(METHOD_NAME_EXCLUDES.contains(methodName)) continue
123127

124128
if(isSetterOrGetterMethod(md)) continue
@@ -129,7 +133,7 @@ class TransactionalTransform implements ASTTransformation{
129133
if(hasAnnotation(md, DelegatingMethod.class)) continue
130134
weaveTransactionalMethod(source, classNode, annotationNode, md);
131135
}
132-
else if("setup".equals(methodName) && isSpockTest(classNode)) {
136+
else if(("setup".equals(methodName) || "cleanup".equals(methodName)) && isSpockTest(classNode)) {
133137
def requiresNewTransaction = new AnnotationNode(annotationNode.classNode)
134138
requiresNewTransaction.addMember("propagation", new PropertyExpression(new ClassExpression(ClassHelper.make(Propagation.class)), "REQUIRES_NEW"))
135139
weaveTransactionalMethod(source, classNode, requiresNewTransaction, md, "execute")

grails-test-suite-uber/src/test/groovy/grails/transaction/TransactionalTransformSpec.groovy

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ class TransactionalTransformSpec extends Specification {
6060
6161
}
6262
63+
def cleanup() {
64+
65+
}
66+
6367
void "my test method"() {
6468
expect:
6569
1 == 1
@@ -72,7 +76,45 @@ class TransactionalTransformSpec extends Specification {
7276
TransactionManagerAware.isAssignableFrom(mySpec)
7377
mySpec.getDeclaredMethod('setup')
7478
mySpec.getDeclaredMethod('$tt__setup', TransactionStatus)
79+
mySpec.getDeclaredMethod('cleanup')
80+
mySpec.getDeclaredMethod('$tt__cleanup', TransactionStatus)
81+
82+
mySpec.getDeclaredMethod('$spock_feature_0_0')
83+
mySpec.getDeclaredMethod('$tt__$spock_feature_0_0', TransactionStatus)
84+
}
85+
86+
void "Test @Rollback when applied to Spock specifications and where blocks"() {
87+
when:"A new instance of a class with a @Transactional method is created that subclasses another transactional class"
88+
Class mySpec = new GroovyShell().evaluate('''
89+
import grails.test.mixin.integration.Integration
90+
import grails.transaction.Rollback
91+
import spock.lang.Specification
92+
93+
@Integration
94+
@Rollback
95+
class DemoSpec extends Specification {
96+
97+
def "test toUpperCase"() {
98+
given:
99+
def result = value.toUpperCase()
100+
101+
expect:
102+
result == expectedResult
103+
104+
where:
105+
value | expectedResult
106+
'King Crimson' | 'KING CRIMSON\'
107+
'Riverside' | 'RIVERSIDE\'
108+
}
109+
}
110+
DemoSpec
111+
''')
112+
113+
then:"It implements TransactionManagerAware"
114+
TransactionManagerAware.isAssignableFrom(mySpec)
75115
mySpec.getDeclaredMethod('$spock_feature_0_0')
116+
mySpec.getDeclaredMethod('$spock_feature_0_0prov0')
117+
!mySpec.getDeclaredMethod('$tt__$spock_feature_0_0prov0')
76118
mySpec.getDeclaredMethod('$tt__$spock_feature_0_0', TransactionStatus)
77119
}
78120

0 commit comments

Comments
 (0)