Skip to content

Commit 015d66c

Browse files
committed
fix for "Using @testfor without @mock requires @before annotation on setUp method"
1 parent 9a38166 commit 015d66c

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

grails-plugin-testing/src/main/groovy/org/codehaus/groovy/grails/compiler/injection/test/TestForTransformation.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.codehaus.groovy.grails.plugins.web.filters.FiltersConfigArtefactHandler;
4747
import org.codehaus.groovy.syntax.Token;
4848
import org.codehaus.groovy.transform.GroovyASTTransformation;
49+
import org.junit.After;
4950
import org.junit.Before;
5051
import org.junit.Test;
5152
import org.springframework.core.io.FileSystemResource;
@@ -86,6 +87,10 @@ public class TestForTransformation extends TestMixinTransformation {
8687
public static final ClassNode BEFORE_CLASS_NODE = new ClassNode(Before.class);
8788
public static final AnnotationNode BEFORE_ANNOTATION = new AnnotationNode(BEFORE_CLASS_NODE);
8889

90+
public static final ClassNode AFTER_CLASS_NODE = new ClassNode(After.class);
91+
public static final AnnotationNode AFTER_ANNOTATION = new AnnotationNode(AFTER_CLASS_NODE);
92+
93+
8994
public static final AnnotationNode TEST_ANNOTATION = new AnnotationNode(new ClassNode(Test.class));
9095
public static final ClassNode GROOVY_TEST_CASE_CLASS = new ClassNode(GroovyTestCase.class);
9196

@@ -187,6 +192,8 @@ else if (isSpock) {
187192
* @param ce The class expression that represents the class to test
188193
*/
189194
public void testFor(ClassNode classNode, ClassExpression ce) {
195+
196+
autoAnnotateSetupTeardown(classNode);
190197
boolean junit3Test = isJunit3Test(classNode);
191198

192199
// make sure the 'log' property is not the one from GroovyTestCase
@@ -215,6 +222,19 @@ public void testFor(ClassNode classNode, ClassExpression ce) {
215222
}
216223
}
217224

225+
private void autoAnnotateSetupTeardown(ClassNode classNode) {
226+
MethodNode setupMethod = classNode.getMethod(SET_UP_METHOD, GrailsArtefactClassInjector.ZERO_PARAMETERS);
227+
if ( setupMethod != null && setupMethod.getAnnotations(BEFORE_CLASS_NODE).size() == 0) {
228+
setupMethod.addAnnotation(BEFORE_ANNOTATION);
229+
}
230+
231+
MethodNode tearDown = classNode.getMethod(TEAR_DOWN_METHOD, GrailsArtefactClassInjector.ZERO_PARAMETERS);
232+
if ( tearDown != null && tearDown.getAnnotations(AFTER_CLASS_NODE).size() == 0) {
233+
tearDown.addAnnotation(AFTER_ANNOTATION);
234+
}
235+
236+
}
237+
218238
private Map<ClassNode, List<Class>> wovenMixins = new HashMap<ClassNode, List<Class>>();
219239
protected MethodNode weaveMock(ClassNode classNode, ClassExpression value, boolean isClassUnderTest) {
220240

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package grails.test.mixin
18+
19+
import org.junit.Test
20+
import org.junit.Before
21+
22+
/**
23+
*/
24+
@TestFor(FirstController)
25+
class SetupTeardownInvokeTests {
26+
27+
void setUp() {
28+
controller.value = 'World!'
29+
}
30+
31+
void tearDown() {
32+
controller.counter++
33+
}
34+
35+
@Test
36+
void testThatSetupWasInvoked() {
37+
assert controller.value == 'World!'
38+
}
39+
40+
@Test
41+
void testThatSetupWasInvoked2() {
42+
assert controller.counter == 1
43+
}
44+
}
45+
class FirstController {
46+
String value
47+
static int counter = 0
48+
}

0 commit comments

Comments
 (0)