Skip to content

Commit

Permalink
improve detection of possible JUnit 4 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
graemerocher committed Feb 7, 2013
1 parent b7b229d commit fcd546b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ else if (isSpock) {
public void testFor(ClassNode classNode, ClassExpression ce) {

autoAnnotateSetupTeardown(classNode);
boolean junit3Test = isJunit3Test(classNode);
boolean isJunit3Test = isJunit3Test(classNode);

// make sure the 'log' property is not the one from GroovyTestCase
FieldNode log = classNode.getField("log");
Expand All @@ -203,22 +203,31 @@ public void testFor(ClassNode classNode, ClassExpression ce) {
}
boolean isSpockTest = isSpockTest(classNode);

if (!isSpockTest && !junit3Test) {
boolean isJunit4 = !isSpockTest && !isJunit3Test;
if (isJunit4) {
// assume JUnit 4
Map<String, MethodNode> declaredMethodsMap = classNode.getDeclaredMethodsMap();
boolean hasTestMethods = false;
for (String methodName : declaredMethodsMap.keySet()) {
MethodNode methodNode = declaredMethodsMap.get(methodName);
if (isCandidateMethod(methodNode) && methodNode.getName().startsWith("test")) {
if (methodNode.getAnnotations().size()==0) {
methodNode.addAnnotation(TEST_ANNOTATION);
hasTestMethods = true;
}
}
}
if(!hasTestMethods) {
isJunit4 = false;
}
}

final MethodNode methodToAdd = weaveMock(classNode, ce, true);
if (methodToAdd != null && junit3Test) {
addMethodCallsToMethod(classNode,SET_UP_METHOD, Arrays.asList(methodToAdd));
if(isJunit4 || isJunit3Test || isSpockTest) {

final MethodNode methodToAdd = weaveMock(classNode, ce, true);
if (methodToAdd != null && isJunit3Test) {
addMethodCallsToMethod(classNode,SET_UP_METHOD, Arrays.asList(methodToAdd));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,45 +35,7 @@ class TestForSpec extends Specification{
test.retrieveLog() instanceof Log
}

void "Test that TestFor doesn't apply if the test is not JUnit 4, Junit 4 or Spock"() {
when:
def test = invalidTest

then:
test != null
test.getClass().getDeclaredMethod("testBlah", null).getAnnotation(Test.class) == null

when:
test.retrieveLog() instanceof Log

then:
thrown MissingPropertyException

}

def getInvalidTest() {
final gcl = new GroovyClassLoader()
gcl.parseClass('''
class SimpleController {
def index = {
render "Hello"
}
}
''')
gcl.parseClass('''
import grails.test.mixin.*

@TestFor(SimpleController)
class ControllerHelper {
void testBlah() {}
def retrieveLog() { log }
}
''').newInstance()
}

def getSpockTest() {
final gcl = new GroovyClassLoader()
gcl.parseClass('''
Expand Down

0 comments on commit fcd546b

Please sign in to comment.