Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 /*
  * Copyright 2003-2007 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 org.codehaus.groovy.vmplugin.v5;
Java 5 code for working with JUnit 4 tests.

Author(s):
Paul King
public class JUnit4Utils {

    
Utility method to check via reflection if the parsed class appears to be a JUnit4 test.

Parameters:
scriptClass the class we want to check
loader the GroovyClassLoader to use to find classes
Returns:
true if the class appears to be a test
    static Boolean realIsJUnit4Test(Class scriptClassGroovyClassLoader loader) {
        // check if there are appropriate class or method annotations
        // that suggest we have a JUnit 4 test
        boolean isTest = false;
        try {
            try {
                Class runWithAnnotationClass = loader.loadClass("org.junit.runner.RunWith");
                Annotation annotation = scriptClass.getAnnotation(runWithAnnotationClass);
                if (annotation != null) {
                    isTest = true;
                } else {
                    Class testAnnotationClass = loader.loadClass("org.junit.Test");
                    Method[] methods = scriptClass.getMethods();
                    for (int i = 0; i < methods.lengthi++) {
                        Method method = methods[i];
                        annotation = method.getAnnotation(testAnnotationClass);
                        if (annotation != null) {
                            isTest = true;
                            break;
                        }
                    }
                }
            } catch (ClassNotFoundException e) {
                // fall through
            }
        } catch (Throwable e) {
            // fall through
        }
        return isTest ? . : .;
    }

    
Utility method to run a JUnit4 test.

Parameters:
scriptClass the class we want to run as a test
Returns:
the result of running the test
    static Object realRunJUnit4Test(Class scriptClass) {
        // invoke through reflection to eliminate mandatory JUnit 4 jar dependency
        try {
            Object result = InvokerHelper.invokeStaticMethod("org.junit.runner.JUnitCore",
                    "runClasses"new Object[]{scriptClass});
            ..print("JUnit 4 Runner, Tests: " + InvokerHelper.getProperty(result"runCount"));
            ..print(", Failures: " + InvokerHelper.getProperty(result"failureCount"));
            ..println(", Time: " + InvokerHelper.getProperty(result"runTime"));
            List failures = (List) InvokerHelper.getProperty(result"failures");
            for (int i = 0; i < failures.size(); i++) {
                Object f = failures.get(i);
                ..println("Test Failure: " + InvokerHelper.getProperty(f"description"));
                ..println(InvokerHelper.getProperty(f"trace"));
            }
            return result;
        } catch (ClassNotFoundException e) {
            throw new GroovyRuntimeException("Error running JUnit 4 test.");
        }
    }
New to GrepCode? Check out our FAQ X