예제 #1
0
  /**
   * Main entry point for the calling the TestForTransformation programmatically.
   *
   * @param classNode The class node that represents th test
   * @param ce The class expression that represents the class to test
   */
  public void testFor(ClassNode classNode, ClassExpression ce) {
    boolean junit3Test = isJunit3Test(classNode);

    // make sure the 'log' property is not the one from GroovyTestCase
    FieldNode log = classNode.getField("log");
    if (log == null || log.getDeclaringClass().equals(GROOVY_TEST_CASE_CLASS)) {
      LoggingTransformer.addLogField(classNode, classNode.getName());
    }
    boolean isSpockTest = isSpockTest(classNode);

    if (!isSpockTest && !junit3Test) {
      // assume JUnit 4
      Map<String, MethodNode> declaredMethodsMap = classNode.getDeclaredMethodsMap();
      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);
          }
        }
      }
    }

    final MethodNode methodToAdd = weaveMock(classNode, ce, true);
    if (methodToAdd != null && junit3Test) {
      addMethodCallsToMethod(classNode, SET_UP_METHOD, Arrays.asList(methodToAdd));
    }
  }
예제 #2
0
 private BlockStatement getJunit4Setup(ClassNode classNode) {
   MethodNode setupMethod =
       classNode.getMethod(SET_UP_METHOD, GrailsArtefactClassInjector.ZERO_PARAMETERS);
   if (setupMethod == null) {
     setupMethod =
         new MethodNode(
             SET_UP_METHOD,
             Modifier.PUBLIC,
             ClassHelper.VOID_TYPE,
             GrailsArtefactClassInjector.ZERO_PARAMETERS,
             null,
             new BlockStatement());
     setupMethod.addAnnotation(MIXIN_METHOD_ANNOTATION);
     classNode.addMethod(setupMethod);
   }
   if (setupMethod.getAnnotations(BEFORE_CLASS_NODE).size() == 0) {
     setupMethod.addAnnotation(BEFORE_ANNOTATION);
   }
   return getOrCreateMethodBody(classNode, setupMethod, SET_UP_METHOD);
 }