Groovy testing library providing JUnit-based testing utilities including test cases, assertions, and mock/stub frameworks
npx @tessl/cli install tessl/maven-org-codehaus-groovy--groovy-test@3.0.0The Groovy testing library provides comprehensive testing utilities for Groovy applications, extending JUnit functionality with Groovy-specific features. It includes JUnit 3 base classes, JUnit 4+ compatible assertion methods, a complete mock/stub framework, AST transformations for test-driven development, and utilities for script testing and test suite management.
implementation 'org.codehaus.groovy:groovy-test:3.0.25'// JUnit 3 style testing
import groovy.test.GroovyTestCase
import groovy.test.GroovyShellTestCase
// JUnit 4+ assertions
import static groovy.test.GroovyAssert.*
// Mock/Stub framework
import groovy.mock.interceptor.MockFor
import groovy.mock.interceptor.StubFor
// Test annotations
import groovy.test.NotYetImplemented
// Test suites
import groovy.test.GroovyTestSuite
import groovy.test.AllTestSuite
// Utilities
import groovy.test.StringTestUtilFor Java projects:
// JUnit 4+ assertions
import static groovy.test.GroovyAssert.*;
// Test annotations
import groovy.test.NotYetImplemented;
// Test suites
import groovy.test.GroovyTestSuite;
import groovy.test.AllTestSuite;import groovy.test.GroovyTestCase
import groovy.mock.interceptor.MockFor
// JUnit 3 style test case
class MyServiceTest extends GroovyTestCase {
void testSomething() {
def result = myService.calculate(10, 20)
assertEquals(30, result)
assertScript "assert 10 + 20 == 30"
// Test failure expectations
shouldFail(IllegalArgumentException) {
myService.calculate(-1, 0)
}
}
void testWithMock() {
def mock = new MockFor(Database)
mock.demand.findUser { id -> [name: "Test User", id: id] }
mock.use {
def database = new Database()
def service = new UserService(database)
def user = service.getUser(123)
assertEquals("Test User", user.name)
}
}
}Groovy Test is organized around several key components:
GroovyTestCase provides JUnit 3 compatibility with Groovy-aware assertionsGroovyAssert provides JUnit 4+ compatible static assertion methodsMockFor (strict) and StubFor (loose) expectations@NotYetImplemented annotation for test-driven development workflowsJUnit 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.
class GroovyTestCase extends TestCase {
protected void assertArrayEquals(Object[] expected, Object[] value);
protected void assertScript(String script) throws Exception;
protected String shouldFail(Closure code);
protected String shouldFail(Class clazz, Closure code);
public static boolean notYetImplemented(Object caller);
}
class GroovyAssert extends Assert {
public static void assertScript(String script) throws Exception;
public static Throwable shouldFail(Closure code);
public static Throwable shouldFail(Class clazz, Closure code);
public static Throwable shouldFailWithCause(Class expectedCause, Closure code);
public static boolean notYetImplemented(Object caller);
}Comprehensive mocking framework supporting both strict (ordered) and loose (unordered) expectations. Uses metaclass interception to mock final classes and methods, with support for constructor interception and partial mocking.
class MockFor {
MockFor(Class clazz, boolean interceptConstruction = false);
void use(Closure closure);
def ignore(Object filter, Closure filterBehavior = null);
GroovyObject proxyInstance(args = null);
}
class StubFor {
StubFor(Class clazz, boolean interceptConstruction = false);
void use(Closure closure);
void verify();
GroovyObject proxyInstance(args = null);
}Utilities for collecting and running multiple test files, with support for pattern matching and integration with Java IDEs. Includes adapters for running Groovy scripts as JUnit tests.
class GroovyTestSuite extends TestSuite {
static Test suite();
void loadTestSuite();
}
class AllTestSuite extends TestSuite {
static Test suite();
static Test suite(String basedir, String pattern);
static Test suite(String basedir, String pattern, String excludesPattern);
}Annotation-based test enhancements including the @NotYetImplemented transformation for test-driven development. Automatically inverts test results to support "failing tests first" development patterns.
@interface NotYetImplemented {
Class<? extends AssertionError> exception() default AssertionError.class;
}interface Test {
int countTestCases();
void run(TestResult result);
}
class TestSuite implements Test {
void addTest(Test test);
void addTestSuite(Class<? extends TestCase> testClass);
}
class Demand {
// Dynamic method recording via invokeMethod
Object invokeMethod(String methodName, Object args);
void verify(List calls);
}
class GroovyShell {
Object evaluate(String script);
Script parse(String script);
Script parse(String script, String fileName);
void setProperty(String property, Object newValue);
}
class Binding {
Map variables;
void setVariable(String name, Object value);
Object getVariable(String name);
}