Permalink
Browse files

fix for "Using @Testfor without @Mock requires @Before annotation on …

…setUp method"
  • Loading branch information...
1 parent 9a38166 commit 015d66ce7a7dcf4ed182bf97562c2584ecc72f61 @graemerocher graemerocher committed Nov 28, 2012
@@ -46,6 +46,7 @@
import org.codehaus.groovy.grails.plugins.web.filters.FiltersConfigArtefactHandler;
import org.codehaus.groovy.syntax.Token;
import org.codehaus.groovy.transform.GroovyASTTransformation;
+import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.io.FileSystemResource;
@@ -86,6 +87,10 @@
public static final ClassNode BEFORE_CLASS_NODE = new ClassNode(Before.class);
public static final AnnotationNode BEFORE_ANNOTATION = new AnnotationNode(BEFORE_CLASS_NODE);
+ public static final ClassNode AFTER_CLASS_NODE = new ClassNode(After.class);
+ public static final AnnotationNode AFTER_ANNOTATION = new AnnotationNode(AFTER_CLASS_NODE);
+
+
public static final AnnotationNode TEST_ANNOTATION = new AnnotationNode(new ClassNode(Test.class));
public static final ClassNode GROOVY_TEST_CASE_CLASS = new ClassNode(GroovyTestCase.class);
@@ -187,6 +192,8 @@ else if (isSpock) {
* @param ce The class expression that represents the class to test
*/
public void testFor(ClassNode classNode, ClassExpression ce) {
+
+ autoAnnotateSetupTeardown(classNode);
boolean junit3Test = isJunit3Test(classNode);
// make sure the 'log' property is not the one from GroovyTestCase
@@ -215,6 +222,19 @@ public void testFor(ClassNode classNode, ClassExpression ce) {
}
}
+ private void autoAnnotateSetupTeardown(ClassNode classNode) {
+ MethodNode setupMethod = classNode.getMethod(SET_UP_METHOD, GrailsArtefactClassInjector.ZERO_PARAMETERS);
+ if ( setupMethod != null && setupMethod.getAnnotations(BEFORE_CLASS_NODE).size() == 0) {
+ setupMethod.addAnnotation(BEFORE_ANNOTATION);
+ }
+
+ MethodNode tearDown = classNode.getMethod(TEAR_DOWN_METHOD, GrailsArtefactClassInjector.ZERO_PARAMETERS);
+ if ( tearDown != null && tearDown.getAnnotations(AFTER_CLASS_NODE).size() == 0) {
+ tearDown.addAnnotation(AFTER_ANNOTATION);
+ }
+
+ }
+
private Map<ClassNode, List<Class>> wovenMixins = new HashMap<ClassNode, List<Class>>();
protected MethodNode weaveMock(ClassNode classNode, ClassExpression value, boolean isClassUnderTest) {
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2012 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package grails.test.mixin
+
+import org.junit.Test
+import org.junit.Before
+
+/**
+ */
+@TestFor(FirstController)
+class SetupTeardownInvokeTests {
+
+ void setUp() {
+ controller.value = 'World!'
+ }
+
+ void tearDown() {
+ controller.counter++
+ }
+
+ @Test
+ void testThatSetupWasInvoked() {
+ assert controller.value == 'World!'
+ }
+
+ @Test
+ void testThatSetupWasInvoked2() {
+ assert controller.counter == 1
+ }
+}
+class FirstController {
+ String value
+ static int counter = 0
+}

0 comments on commit 015d66c

Please sign in to comment.