Package groovy.util
Class GroovyTestCase
java.lang.Object
junit.framework.Assert
junit.framework.TestCase
groovy.util.GroovyTestCase
- All Implemented Interfaces:
junit.framework.Test
public class GroovyTestCase
extends junit.framework.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.
- Version:
- $Revision$
- Author:
- bob mcwhirter, James Strachan, Dierk Koenig (the notYetImplemented feature, changes to shouldFail)
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidassertArrayEquals(Object[] expected, Object[] value) Asserts that the arrays are equivalent and contain the same valuesprotected voidassertContains(char expected, char[] array) Asserts that the array of characters contains a given charprotected voidassertContains(int expected, int[] array) Asserts that the array of ints contains a given intstatic voidassertEquals(Object expected, Object actual) static voidassertEquals(String message, Object expected, Object actual) static voidassertEquals(String expected, String actual) protected voidassertInspect(Object value, String expected) Asserts that the value of inspect() on the given object matches the given text stringprotected voidassertLength(int length, char[] array) Asserts that the array of characters has a given lengthprotected voidassertLength(int length, int[] array) Asserts that the array of ints has a given lengthprotected voidassertLength(int length, Object[] array) Asserts that the array of objects has a given lengthprotected voidassertScript(String script) Asserts that the script runs without any exceptionsprotected voidassertToString(Object value, String expected) Asserts that the value of toString() on the given object matches the given text stringprotected StringReturns a copy of a string in which all EOLs are \n.getName()Overload the getName() method to make the test cases look more like AgileDox (thanks to Joe Walnes for this tip!)protected StringbooleanConvenience method for subclasses of GroovyTestCase, identical tostatic booleannotYetImplemented(junit.framework.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:protected StringshouldFail(Closure code) Asserts that the given code closure fails when it is evaluated.protected StringshouldFail(Class clazz, Closure code) Asserts that the given code closure fails when it is evaluated and that a particular exception type is thrown.protected StringshouldFailWithCause(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.Methods inherited from class junit.framework.TestCase
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, countTestCases, createResult, fail, fail, failNotEquals, failNotSame, failSame, format, run, run, runBare, runTest, setName, setUp, tearDown, toString
-
Field Details
-
log
-
TEST_SCRIPT_NAME_PREFIX
- See Also:
-
-
Constructor Details
-
GroovyTestCase
public GroovyTestCase()
-
-
Method Details
-
getName
Overload the getName() method to make the test cases look more like AgileDox (thanks to Joe Walnes for this tip!)- Overrides:
getNamein classjunit.framework.TestCase
-
getMethodName
-
assertArrayEquals
Asserts that the arrays are equivalent and contain the same values- Parameters:
expected-value-
-
assertLength
protected void assertLength(int length, char[] array) Asserts that the array of characters has a given length- Parameters:
length- expected lengtharray- the array
-
assertLength
protected void assertLength(int length, int[] array) Asserts that the array of ints has a given length- Parameters:
length- expected lengtharray- the array
-
assertLength
Asserts that the array of objects has a given length- Parameters:
length- expected lengtharray- the array
-
assertContains
protected void assertContains(char expected, char[] array) Asserts that the array of characters contains a given char- Parameters:
expected- expected character to be foundarray- the array
-
assertContains
protected void assertContains(int expected, int[] array) Asserts that the array of ints contains a given int- Parameters:
expected- expected intarray- the array
-
assertToString
Asserts that the value of toString() on the given object matches the given text string- Parameters:
value- the object to be output to the consoleexpected- the expected String representation
-
assertInspect
Asserts that the value of inspect() on the given object matches the given text string- Parameters:
value- the object to be output to the consoleexpected- the expected String representation
-
assertScript
Asserts that the script runs without any exceptions- Parameters:
script- the script that should pass without any exception thrown- Throws:
Exception
-
getTestClassName
-
shouldFail
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
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 exceptioncode- the closure that should fail- Returns:
- the message of the expected Throwable
-
shouldFailWithCause
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 exceptioncode- the closure that should fail- Returns:
- the message of the expected Throwable
-
fixEOLs
Returns a copy of a string in which all EOLs are \n. -
notYetImplemented
public static boolean notYetImplemented(junit.framework.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:
when not itself already in the call stack
-
notYetImplemented
public boolean notYetImplemented()Convenience method for subclasses of GroovyTestCase, identical toGroovyTestCase.notYetImplemented(this);
.- Returns:
when not itself already in the call stack - See Also:
-
assertEquals
-
assertEquals
-
assertEquals
-