Groovy Documentation

## groovy.util
[Java] Class GroovyTestCase

    java.lang.Object
      junit.framework.TestCase
          groovy.util.GroovyTestCase

    public class GroovyTestCase
    extends TestCase

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.

Authors:
  bob mcwhirter
  James Strachan
  Dierk Koenig (the notYetImplemented feature, changes to shouldFail)

Version:
  \$Revision\$

Field Summary
`static java.lang.String` | `TEST_SCRIPT_NAME_PREFIX`
`protected static Logger` | `log`

Method Summary
`protected void` | `assertArrayEquals(java.lang.Object[] expected, java.lang.Object[] value)` Asserts that the arrays are equivalent and contain the same values
`protected void` | `assertContains(char expected, char[] array)` Asserts that the array of characters contains a given char
`protected void` | `assertContains(int expected, int[] array)` Asserts that the array of ints contains a given int
`static void` | `assertEquals(java.lang.String message, java.lang.Object expected, java.lang.Object actual)`
`static void` | `assertEquals(java.lang.Object expected, java.lang.Object actual)`
`static void` | `assertEquals(java.lang.String expected, java.lang.String actual)`
`protected void` | `assertInspect(java.lang.Object value, java.lang.String expected)` Asserts that the value of inspect() on the given object matches the given text string
`protected void` | `assertLength(int length, char[] array)` Asserts that the array of characters has a given length
`protected void` | `assertLength(int length, int[] array)` Asserts that the array of ints has a given length
`protected void` | `assertLength(int length, java.lang.Object[] array)` Asserts that the array of objects has a given length
`protected void` | `assertScript(java.lang.String script)` Asserts that the script runs without any exceptions
`protected void` | `assertToString(java.lang.Object value, java.lang.String expected)` Asserts that the value of toString() on the given object matches the given text string
`protected java.lang.String` | `fixEOLs(java.lang.String value)` Returns a copy of a string in which all EOLs are \n.
`java.lang.String` | `getMethodName()`
`java.lang.String` | `getName()` Overload the getName() method to make the test cases look more like AgileDox (thanks to Joe Walnes for this tip!)
`protected java.lang.String` | `getTestClassName()`
`static boolean` | `notYetImplemented(TestCase caller)` Runs the calling JUnit test again and fails only if it unexpectedly runs.

`boolean`

`notYetImplemented()`

Convenience method for subclasses of GroovyTestCase, identical to

     GroovyTestCase.notYetImplemented(this);

.

`protected java.lang.String` | `shouldFail(Closure code)` Asserts that the given code closure fails when it is evaluated.
`protected java.lang.String` | `shouldFail(java.lang.Class clazz, Closure code)` Asserts that the given code closure fails when it is evaluated and that a particular exception type is thrown.
`protected java.lang.String` | `shouldFailWithCause(java.lang.Class clazz, Closure code)` Asserts that the given code closure fails when it is evaluated and that a particular exception type can be attributed to the cause.

Field Detail

### TEST_SCRIPT_NAME_PREFIX

    public static final java.lang.String TEST_SCRIPT_NAME_PREFIX

### log

    protected static Logger log

Method Detail

### assertArrayEquals

    protected void assertArrayEquals(java.lang.Object[] expected, java.lang.Object[] value)

  Asserts that the arrays are equivalent and contain the same values
  Parameters:
    expected
    value

### assertContains

    protected void assertContains(char expected, char[] array)

  Asserts that the array of characters contains a given char
  Parameters:
    `expected` - expected character to be found
    `array` - the array

### assertContains

    protected void assertContains(int expected, int[] array)

  Asserts that the array of ints contains a given int
  Parameters:
    `expected` - expected int
    `array` - the array

### assertEquals

    public static void assertEquals(java.lang.String message, java.lang.Object expected, java.lang.Object actual)

### assertEquals

    public static void assertEquals(java.lang.Object expected, java.lang.Object actual)

### assertEquals

    public static void assertEquals(java.lang.String expected, java.lang.String actual)

### assertInspect

    protected void assertInspect(java.lang.Object value, java.lang.String expected)

  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

### assertLength

    protected void assertLength(int length, char[] array)

  Asserts that the array of characters has a given length
  Parameters:
    `length` - expected length
    `array` - the array

### assertLength

    protected void assertLength(int length, int[] array)

  Asserts that the array of ints has a given length
  Parameters:
    `length` - expected length
    `array` - the array

### assertLength

    protected void assertLength(int length, java.lang.Object[] array)

  Asserts that the array of objects has a given length
  Parameters:
    `length` - expected length
    `array` - the array

### assertScript

    protected void assertScript(java.lang.String script)

  Asserts that the script runs without any exceptions
  Parameters:
    `script` - the script that should pass without any exception thrown

### assertToString

    protected void assertToString(java.lang.Object value, java.lang.String expected)

  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

### fixEOLs

    protected java.lang.String fixEOLs(java.lang.String value)

  Returns a copy of a string in which all EOLs are \n.

### getMethodName

    public java.lang.String getMethodName()

### getName

    public java.lang.String getName()

  Overload the getName() method to make the test cases look more like AgileDox (thanks to Joe Walnes for this tip!)

### getTestClassName

    protected java.lang.String getTestClassName()

### notYetImplemented

    public static boolean notYetImplemented(TestCase caller)

  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

### notYetImplemented

    public boolean notYetImplemented()

  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)

### shouldFail

    protected java.lang.String shouldFail(Closure code)

  Asserts that the given code closure fails when it is evaluated.
  Parameters:
    `code` - the closure that should fail
  Returns:
    the message of the thrown Throwable

### shouldFail

    protected java.lang.String shouldFail(java.lang.Class clazz, Closure code)

  Asserts that the given code closure fails when it is evaluated and that a particular exception type is thrown.
  Parameters:
    `clazz` - the class of the expected exception
    `code` - the closure that should fail
  Returns:
    the message of the expected Throwable

### shouldFailWithCause

    protected java.lang.String shouldFailWithCause(java.lang.Class clazz, Closure code)

  Asserts that the given code closure fails when it is evaluated and that a particular exception type can be attributed to the cause. The expected exception class is compared recursively with any nested exceptions using getCause() until either a match is found or no more nested exceptions exist.

  If a match is found the error message associated with the matching exception is returned. If no match was found the method will fail.
  Parameters:
    `clazz` - the class of the expected exception
    `code` - the closure that should fail
  Returns:
    the message of the expected Throwable

Groovy Documentation