or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

ast-transformations.mdindex.mdmock-stub.mdtest-cases.mdtest-suites.md
tile.json

tessl/maven-org-codehaus-groovy--groovy-test

Groovy testing library providing JUnit-based testing utilities including test cases, assertions, and mock/stub frameworks

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.codehaus.groovy/groovy-test@3.0.x

To install, run

npx @tessl/cli install tessl/maven-org-codehaus-groovy--groovy-test@3.0.0

index.mddocs/

Groovy Test

The 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.

Package Information

  • Package Name: groovy-test
  • Package Type: maven
  • Language: Groovy/Java
  • Installation: implementation 'org.codehaus.groovy:groovy-test:3.0.25'

Core Imports

// 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.StringTestUtil

For 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;

Basic Usage

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)
        }
    }
}

Architecture

Groovy Test is organized around several key components:

  • Test Case Classes: GroovyTestCase provides JUnit 3 compatibility with Groovy-aware assertions
  • Static Assertions: GroovyAssert provides JUnit 4+ compatible static assertion methods
  • Mock/Stub Framework: Interceptor-based mocking with MockFor (strict) and StubFor (loose) expectations
  • Test Suite Management: Classes for collecting and running multiple test files from directories
  • AST Transformations: @NotYetImplemented annotation for test-driven development workflows
  • Script Testing: Adapters for running Groovy scripts as JUnit tests

Capabilities

Test Cases and Assertions

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.

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);
}

Test Cases and Assertions

Mock and Stub Framework

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);
}

Mock and Stub Framework

Test Suite Management

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);
}

Test Suite Management

AST Transformations

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;
}

AST Transformations

Types

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);
}