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 groovy.util;
 
 
A default JUnit TestCase in Groovy. This provides a number of helper methods plus avoids the JUnit restriction of requiring all test* methods to be void return type.

Author(s):
bob mcwhirter
James Strachan
Dierk Koenig (the notYetImplemented feature, changes to shouldFail)
Version:
$Revision: 14149 $
 
 public class GroovyTestCase extends TestCase {
 
     protected static Logger log = Logger.getLogger(GroovyTestCase.class.getName());
     private static int counter;
     public static final String TEST_SCRIPT_NAME_PREFIX = "TestScript";
 
     private boolean useAgileDoxNaming = false;
 
     public GroovyTestCase() {
     }

    
Overload the getName() method to make the test cases look more like AgileDox (thanks to Joe Walnes for this tip!)
 
     public String getName() {
         if () {
             return super.getName().substring(4).replaceAll("([A-Z])"" $1").toLowerCase();
         }
         else {
             return super.getName();
         }
     }
 
     public String getMethodName() {
         return super.getName();
     }

    
Asserts that the arrays are equivalent and contain the same values

Parameters:
expected
value
 
     protected void assertArrayEquals(Object[] expectedObject[] value) {
         String message =
             "expected array: " + InvokerHelper.toString(expected) + " value array: " + InvokerHelper.toString(value);
         assertNotNull(message + ": expected should not be null"expected);
         assertNotNull(message + ": value should not be null"value);
         assertEquals(messageexpected.lengthvalue.length);
         for (int i = 0, size = expected.lengthi < sizei++) {
             assertEquals("value[" + i + "] when " + messageexpected[i], value[i]);
         }
     }

    
Asserts that the array of characters has a given length

Parameters:
length expected length
array the array
 
     protected void assertLength(int lengthchar[] array) {
         assertEquals(lengtharray.length);
     }

    
Asserts that the array of ints has a given length

Parameters:
length expected length
array the array
    protected void assertLength(int lengthint[] array) {
        assertEquals(lengtharray.length);
    }

    
Asserts that the array of objects has a given length

Parameters:
length expected length
array the array
    protected void assertLength(int lengthObject[] array) {
        assertEquals(lengtharray.length);
    }

    
Asserts that the array of characters contains a given char

Parameters:
expected expected character to be found
array the array
    protected void assertContains(char expectedchar[] array) {
        for (int i = 0; i < array.length; ++i) {
            if (array[i] == expected) {
                return;
            }
        }
        StringBuffer message = new StringBuffer();
        message.append(expected).append(" not in {");
        for (int i = 0; i < array.length; ++i) {
            message.append("'").append(array[i]).append("'");
            if (i < (array.length - 1)) {
                message.append(", ");
            }
        }
        message.append(" }");
        fail(message.toString());
    }

    
Asserts that the array of ints contains a given int

Parameters:
expected expected int
array the array
    protected void assertContains(int expectedint[] array) {
        for (int i = 0; i < array.length; ++i) {
            if (array[i] == expected) {
                return;
            }
        }
        StringBuffer message = new StringBuffer();
        message.append(expected).append(" not in {");
        for (int i = 0; i < array.length; ++i) {
            message.append("'").append(array[i]).append("'");
            if (i < (array.length - 1)) {
                message.append(", ");
            }
        }
        message.append(" }");
        fail(message.toString());
    }

    
Asserts that the value of toString() on the given object matches the given text string

Parameters:
value the object to be output to the console
expected the expected String representation
    protected void assertToString(Object valueString expected) {
        Object console = InvokerHelper.invokeMethod(value"toString"null);
        assertEquals("toString() on value: " + valueexpectedconsole);
    }

    
Asserts that the value of inspect() on the given object matches the given text string

Parameters:
value the object to be output to the console
expected the expected String representation
    protected void assertInspect(Object valueString expected) {
        Object console = InvokerHelper.invokeMethod(value"inspect"null);
        assertEquals("inspect() on value: " + valueexpectedconsole);
    }

    
Asserts that the script runs without any exceptions

Parameters:
script the script that should pass without any exception thrown
    protected void assertScript(final String scriptthrows Exception {
        GroovyShell shell = new GroovyShell();
        shell.evaluate(scriptgetTestClassName());
    }
    protected String getTestClassName() {
        return  + getMethodName() + (++) + ".groovy";
    }

    
Asserts that the given code closure fails when it is evaluated

Parameters:
code
Returns:
the message of the thrown Throwable
    protected String shouldFail(Closure code) {
        boolean failed = false;
        String result = null;
        try {
            code.call();
        }
        catch (GroovyRuntimeException gre) {
            failed = true;
            result = ScriptBytecodeAdapter.unwrap(gre).getMessage();
        }
        catch (Throwable e) {
                failed = true;
                result = e.getMessage();
        }
        assertTrue("Closure " + code + " should have failed"failed);
        return result;
    }

    
Asserts that the given code closure fails when it is evaluated and that a particular exception is thrown.

Parameters:
clazz the class of the expected exception
code the closure that should fail
Returns:
the message of the expected Throwable
    protected String shouldFail(Class clazzClosure code) {
        Throwable th = null;
        try {
            code.call();
        } catch (GroovyRuntimeException gre) {
            th = ScriptBytecodeAdapter.unwrap(gre);
        } catch (Throwable e) {
            th = e;
        }
        if (th==null) {
            fail("Closure " + code + " should have failed with an exception of type " + clazz.getName());
        } else if (! clazz.isInstance(th)) {
            fail("Closure " + code + " should have failed with an exception of type " + clazz.getName() + ", instead got Exception " + th);
        }
        return th.getMessage();
    }
    protected String shouldFailWithCause(Class clazzClosure code) {
        Throwable th = null;
        try {
            code.call();
        } catch (GroovyRuntimeException gre) {
            th = gre;
            while (th.getCause()!=null && th.getCause()!=gre){ // if wrapped, find the root cause
                th=th.getCause();
                if (th!=gre && (th instanceof GroovyRuntimeException)) {
                    gre = (GroovyRuntimeExceptionth;
                }
            }
        } catch (Throwable e) {
            th = e;
        }
        if (th==null) {
            fail("Closure " + code + " should have failed with an exception of type " + clazz.getName());
        } else if (! clazz.isInstance(th)) {
            fail("Closure " + code + " should have failed with an exception of type " + clazz.getName() + ", instead got Exception " + th);
        }
        return th.getMessage();
    }

    
Returns a copy of a string in which all EOLs are \n.
    protected String fixEOLsString value )
    {
        return value.replaceAll"(\\r\\n?)|\n""\n" );
    }

    
Runs the calling JUnit test again and fails only if it unexpectedly runs.
This is helpful for tests that don't currently work but should work one day, when the tested functionality has been implemented.
The right way to use it is:
 public void testXXX() {
   if (GroovyTestCase.notYetImplemented(this)) return;
   ... the real (now failing) unit test
 }
 
Idea copied from HtmlUnit (many thanks to Marc Guillemot). Future versions maybe available in the JUnit distro. The purpose of providing a 'static' version is such that you can use the feature even if not subclassing GroovyTestCase.

Returns:
<false> when not itself already in the call stack
    public static boolean notYetImplemented(TestCase caller) {
        if (.get() != null) {
            return false;
        }
        final Method testMethod = findRunningJUnitTestMethod(caller.getClass());
        try {
            .info("Running " + testMethod.getName() + " as not yet implemented");
            testMethod.invoke(caller, (Object[]) new Class[] {});
            fail(testMethod.getName() + " is marked as not yet implemented but passes unexpectedly");
        }
        catch (final Exception e) {
            .info(testMethod.getName() + " fails which is expected as it is not yet implemented");
            // method execution failed, it is really "not yet implemented"
        }
        finally {
            .set(null);
        }
        return true;
    }

    
Convenience method for subclasses of GroovyTestCase, identical to
 GroovyTestCase.notYetImplemented(this); 
.

Returns:
<false> when not itself already in the call stack
See also:
notYetImplemented(junit.framework.TestCase)
    public boolean notYetImplemented() {
        return notYetImplemented(this);
    }

    
From JUnit. Finds from the call stack the active running JUnit test case

Returns:
the test case method
Throws:
java.lang.RuntimeException if no method could be found.
    private static Method findRunningJUnitTestMethod(Class caller) {
        final Class[] args = new Class[] {};
        // search the inial junit test
        final Throwable t = new Exception();
        for (int i=t.getStackTrace().length-1; i>=0; --i) {
            final StackTraceElement element = t.getStackTrace()[i];
            if (element.getClassName().equals(caller.getName())) {
                try {
                    final Method m = caller.getMethod(element.getMethodName(), args);
                    if (isPublicTestMethod(m)) {
                        return m;
                    }
                }
                catch (final Exception e) {
                    // can't access, ignore it
                }
            }
        }
        throw new RuntimeException("No JUnit test case method found in call stack");
    }


    
From Junit. Test if the method is a junit test.

Parameters:
method the method
Returns:
true if this is a junit test.
    private static boolean isPublicTestMethod(final Method method) {
        final String name = method.getName();
        final Class[] parameters = method.getParameterTypes();
        final Class returnType = method.getReturnType();
        return parameters.length == 0 && name.startsWith("test")
            && returnType.equals(.)
            && Modifier.isPublic(method.getModifiers());
    }
    public static void assertEquals(String messageObject expectedObject actual) {
        if (expected == null && actual == null)
			return;
		if (expected != null && DefaultTypeTransformation.compareEqual(expectedactual))
			return;
		failNotEquals(messageexpectedactual);
    }
    public static void assertEquals(Object expectedObject actual) {
	    assertEquals(nullexpectedactual);
	}
	public static void assertEquals(String expectedString actual) {
	    assertEquals(nullexpectedactual);
	}
    private static final ThreadLocal notYetImplementedFlag = new ThreadLocal();
New to GrepCode? Check out our FAQ X