Groovy testing library providing JUnit-based testing utilities including test cases, assertions, and mock/stub frameworks
—
JUnit 3 and JUnit 4+ compatible test cases with Groovy-aware assertion methods. Enhanced equality checking using Groovy's type conversion and additional assertion utilities for arrays, collections, and script execution.
JUnit 3 base class with enhanced Groovy-aware assertion methods and script testing capabilities.
/**
* JUnit 3 base class with Groovy-aware testing capabilities
*/
class GroovyTestCase extends TestCase {
/** Test a Groovy script execution without exceptions */
protected void assertScript(String script) throws Exception;
/** Assert that arrays contain equivalent values */
protected void assertArrayEquals(Object[] expected, Object[] value);
/** Assert array length for different array types */
protected void assertLength(int length, char[] array);
protected void assertLength(int length, int[] array);
protected void assertLength(int length, Object[] array);
/** Assert that arrays contain specific elements */
protected void assertContains(char expected, char[] array);
protected void assertContains(int expected, int[] array);
/** Assert toString() and inspect() output */
protected void assertToString(Object value, String expected);
protected void assertInspect(Object value, String expected);
/** Expect closure to fail and return exception message */
protected String shouldFail(Closure code);
protected String shouldFail(Class clazz, Closure code);
protected String shouldFailWithCause(Class clazz, Closure code);
/** Expect script to fail and return exception message */
protected String shouldFail(String script);
protected String shouldFail(Class clazz, String script);
/** Mark test as not-yet-implemented (static version) */
public static boolean notYetImplemented(Object caller);
/** Mark test as not-yet-implemented (instance version) */
public boolean notYetImplemented();
/** Groovy-aware equality assertion (overrides TestCase) */
public static void assertEquals(String message, Object expected, Object actual);
public static void assertEquals(Object expected, Object actual);
public static void assertEquals(String expected, String actual);
/** Normalize line endings utility */
protected String fixEOLs(String value);
/** Test method name utilities - getName() can be overridden for AgileDox style */
public String getName();
public String getMethodName();
/** Get test class name for script generation */
protected String getTestClassName();
}Usage Examples:
import groovy.test.GroovyTestCase
class MyTest extends GroovyTestCase {
void testBasicAssertions() {
// Groovy-aware equality
assertEquals([1, 2, 3], [1, 2, 3])
assertEquals(new BigDecimal("1.0"), 1.0)
// Array assertions
assertArrayEquals(["a", "b"] as String[], ["a", "b"] as String[])
assertLength(3, [1, 2, 3] as int[])
assertContains(2, [1, 2, 3] as int[])
}
void testScriptExecution() {
// Test that script runs without exception
assertScript '''
def x = 10
def y = 20
assert x + y == 30
'''
}
void testFailureExpectation() {
// Expect any exception
shouldFail {
throw new RuntimeException("Expected failure")
}
// Expect specific exception type
shouldFail(IllegalArgumentException) {
throw new IllegalArgumentException("Bad argument")
}
// Expect exception with specific nested cause
shouldFailWithCause(NumberFormatException) {
try {
Integer.parseInt("not-a-number")
} catch (NumberFormatException e) {
throw new RuntimeException("Wrapper", e)
}
}
}
void testNotYetImplemented() {
if (notYetImplemented()) return
// This test will pass when the feature is implemented
// but currently fails - that's expected behavior
def result = myNewFeature()
assertEquals("expected", result)
}
}Static assertion methods compatible with JUnit 4+ for use in modern testing frameworks.
/**
* JUnit 4+ compatible static assertion methods for Groovy
*/
class GroovyAssert extends Assert {
/** Test script execution without exceptions */
public static void assertScript(String script) throws Exception;
/** Expect closure to fail and return the exception */
public static Throwable shouldFail(Closure code);
public static Throwable shouldFail(Class clazz, Closure code);
/** Expect closure to fail with specific nested cause */
public static Throwable shouldFailWithCause(Class expectedCause, Closure code);
/** Expect script to fail and return the exception */
public static Throwable shouldFail(String script);
public static Throwable shouldFail(Class clazz, String script);
/** Support for not-yet-implemented tests */
public static boolean notYetImplemented(Object caller);
/** JDK version checking utility */
public static boolean isAtLeastJdk(String specVersion);
}Usage Examples:
import static groovy.test.GroovyAssert.*
import org.junit.Test
class MyJUnit4Test {
@Test
void testWithStaticAssertions() {
// Test script execution
assertScript '''
def list = [1, 2, 3]
assert list.size() == 3
assert list.sum() == 6
'''
// Expect failures
def exception = shouldFail(IllegalArgumentException) {
throw new IllegalArgumentException("Test exception")
}
assertEquals("Test exception", exception.message)
}
@Test
void testNotYetImplemented() {
if (notYetImplemented(this)) return
// Implementation pending
fail("Feature not yet implemented")
}
@Test
void testJdkVersion() {
if (isAtLeastJdk("11")) {
// Use JDK 11+ features
def text = """
Multi-line
text block
"""
assertNotNull(text)
}
}
}Test case class with integrated GroovyShell management for script evaluation and binding manipulation.
/**
* Test case with integrated GroovyShell management
*/
class GroovyShellTestCase extends GroovyTestCase {
/** Create new shell instance (override to customize) */
protected GroovyShell createNewShell();
/** Execute closure with temporary variable binding */
protected Object withBinding(Map map, Closure closure);
/** Execute script with temporary variable binding */
protected Object withBinding(Map map, String script);
// All GroovyShell methods available via @Delegate
// evaluate(), parse(), run() etc.
}Usage Examples:
import groovy.test.GroovyShellTestCase
class ShellTest extends GroovyShellTestCase {
void testScriptEvaluation() {
def result = evaluate("10 + 20")
assertEquals(30, result)
def script = """
def multiply(a, b) { a * b }
multiply(6, 7)
"""
assertEquals(42, evaluate(script))
}
void testWithBinding() {
def result = withBinding([x: 100, y: 200]) {
evaluate("x + y")
}
assertEquals(300, result)
def scriptResult = withBinding([name: "World"]) {
"Hello, \${name}!"
}
assertEquals("Hello, World!", scriptResult)
}
protected GroovyShell createNewShell() {
// Customize shell creation
def config = new CompilerConfiguration()
config.scriptBaseClass = MyCustomBaseScript.name
return new GroovyShell(config)
}
}Utility class for string-specific testing operations, particularly multiline string comparisons.
/**
* String testing utilities
*/
class StringTestUtil {
/** Compare multiline strings with line-by-line equality checking */
static void assertMultilineStringsEqual(String a, String b);
}Usage Examples:
import static groovy.test.StringTestUtil.assertMultilineStringsEqual
class TextProcessorTest extends GroovyTestCase {
void testMultilineOutput() {
def processor = new TextProcessor()
def result = processor.format("""
line one
line two
line three
""")
def expected = """
Line One
Line Two
Line Three
"""
// Compare multiline strings with automatic trimming and normalization
assertMultilineStringsEqual(expected, result)
}
}The assertion methods throw standard JUnit exceptions:
AssertionFailedError for failed assertionsComparisonFailure for failed equality assertions with detailed diff informationThe shouldFail methods catch and return exceptions, allowing for detailed exception testing:
def exception = shouldFail(SQLException) {
database.executeInvalidQuery()
}
assertEquals("Invalid SQL syntax", exception.message)
assertEquals("42000", exception.sqlState)Install with Tessl CLI
npx tessl i tessl/maven-org-codehaus-groovy--groovy-test