Groovy testing library providing JUnit-based testing utilities including test cases, assertions, and mock/stub frameworks
—
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.
TestSuite for running individual Groovy test scripts within Java IDEs or as standalone applications.
/**
* TestSuite for running Groovy unit tests in Java IDEs
*/
class GroovyTestSuite extends TestSuite {
/** Create test suite instance */
static Test suite();
/** Load test from system property or command line argument */
void loadTestSuite();
/** Compile Groovy script file to class */
Class compile(String fileName);
/** Command-line entry point */
static void main(String[] args);
}Usage Examples:
// Run from command line
java groovy.test.GroovyTestSuite src/test/MyTest.groovy
// Run with system property
java -Dtest=src/test/MyTest.groovy groovy.test.GroovyTestSuite
// Create programmatically
import groovy.test.GroovyTestSuite
import junit.textui.TestRunner
def suite = GroovyTestSuite.suite()
TestRunner.run(suite)Collects and runs multiple Groovy test files from directories using pattern matching.
/**
* Collects multiple test files from directories with pattern matching
*/
class AllTestSuite extends TestSuite {
/** Create suite with default system properties */
static Test suite();
/** Create suite with custom directory and pattern */
static Test suite(String basedir, String pattern);
/** Create suite with includes and excludes patterns */
static Test suite(String basedir, String pattern, String excludesPattern);
/** Load individual test file */
protected void loadTest(String filename);
/** Compile test file to class */
protected Class compile(String filename);
/** System property for test directory */
static final String SYSPROP_TEST_DIR = "groovy.test.dir";
/** System property for test pattern */
static final String SYSPROP_TEST_PATTERN = "groovy.test.pattern";
/** System property for excludes pattern */
static final String SYSPROP_TEST_EXCLUDES_PATTERN = "groovy.test.excludesPattern";
}Usage Examples:
import groovy.test.AllTestSuite
import junit.textui.TestRunner
// Use default settings (./test/ directory, **/*Test.groovy pattern)
def suite = AllTestSuite.suite()
TestRunner.run(suite)
// Custom directory and pattern
def customSuite = AllTestSuite.suite("src/test", "**/*Spec.groovy")
TestRunner.run(customSuite)
// With excludes pattern
def filteredSuite = AllTestSuite.suite(
"test",
"**/*Test.groovy",
"**/Abstract*Test.groovy"
)
TestRunner.run(filteredSuite)
// Configure via system properties
System.setProperty("groovy.test.dir", "src/test/groovy")
System.setProperty("groovy.test.pattern", "**/*Specification.groovy")
System.setProperty("groovy.test.excludesPattern", "**/integration/**")
def propSuite = AllTestSuite.suite()
TestRunner.run(propSuite)JUnit Test adapter for running Groovy Script classes as test cases.
/**
* JUnit Test adapter for Groovy Script classes
*/
class ScriptTestAdapter implements Test {
/** Create adapter for script class with arguments */
ScriptTestAdapter(Class scriptClass, String[] arguments);
/** Return test count (always 1) */
int countTestCases();
/** Execute the script as a test */
void run(TestResult result);
/** Return descriptive test name */
String toString();
}Usage Examples:
import org.apache.groovy.test.ScriptTestAdapter
import junit.framework.TestResult
// Create adapter for script class
def scriptClass = new GroovyClassLoader().parseClass("""
println "Running test script"
assert 2 + 2 == 4
assert "hello".toUpperCase() == "HELLO"
println "Test script completed successfully"
""")
def adapter = new ScriptTestAdapter(scriptClass, [] as String[])
def result = new TestResult()
adapter.run(result)
println "Test count: ${adapter.countTestCases()}"
println "Errors: ${result.errorCount()}"
println "Failures: ${result.failureCount()}"
// Use with TestSuite
import junit.framework.TestSuite
def suite = new TestSuite()
suite.addTest(adapter)Configure your IDE to run Groovy tests using these test suites:
// IntelliJ IDEA / Eclipse: Create JUnit run configuration
// Main class: groovy.test.AllTestSuite
// VM options: -Dgroovy.test.dir=src/test/groovy -Dgroovy.test.pattern=**/*Test.groovy
// Or use GroovyTestSuite for individual files
// Main class: groovy.test.GroovyTestSuite
// Program arguments: src/test/groovy/MySpecificTest.groovy// build.gradle
test {
// Run Groovy tests alongside Java tests
include '**/*Test.groovy'
include '**/*Spec.groovy'
systemProperty 'groovy.test.dir', 'src/test/groovy'
systemProperty 'groovy.test.pattern', '**/*Test.groovy'
}
// Custom test task for AllTestSuite
task groovyTests(type: JavaExec) {
classpath = sourceSets.test.runtimeClasspath
main = 'groovy.test.AllTestSuite'
systemProperty 'groovy.test.dir', 'src/test/groovy'
systemProperty 'groovy.test.pattern', '**/*Test.groovy'
}<!-- pom.xml -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*Test.groovy</include>
<include>**/*Test.java</include>
</includes>
<systemProperties>
<property>
<name>groovy.test.dir</name>
<value>src/test/groovy</value>
</property>
<property>
<name>groovy.test.pattern</name>
<value>**/*Test.groovy</value>
</property>
</systemProperties>
</configuration>
</plugin>Test files collected by AllTestSuite must meet one of these criteria:
junit.framework.TestCase or groovy.test.GroovyTestCaseScriptTestAdapter// Valid TestCase class
class MyFeatureTest extends GroovyTestCase {
void testSomething() {
assertEquals(expected, actual)
}
}
// Valid test script
def calculator = new Calculator()
assert calculator.add(2, 3) == 5
assert calculator.multiply(4, 5) == 20
println "Calculator tests passed"Configure test collection behavior using system properties:
| Property | Default | Description |
|---|---|---|
groovy.test.dir | "./test/" | Base directory for test collection |
groovy.test.pattern | "**/*Test.groovy" | Include pattern for test files |
groovy.test.excludesPattern | "" | Exclude pattern for test files |
Pattern Examples:
// Include all Groovy files ending with Test or Spec
"**/*{Test,Spec}.groovy"
// Include only unit tests, exclude integration tests
"**/*Test.groovy" // include
"**/integration/**" // exclude
// Include nested directories
"**/test/**/*Test.groovy"
// Exclude abstract base classes
"**/*Test.groovy" // include
"**/Abstract*Test.groovy" // excludeTest suite errors are reported through the JUnit TestResult mechanism:
RuntimeException with detailed error messagesIOException when test files cannot be readRuntimeException when files don't extend TestCase or Script// Error handling example
def suite = AllTestSuite.suite("invalid/path", "*.groovy")
def result = new TestResult()
suite.run(result)
if (result.errorCount() > 0) {
result.errors().each { error ->
println "Error in ${error.failedTest()}: ${error.thrownException().message}"
}
}Install with Tessl CLI
npx tessl i tessl/maven-org-codehaus-groovy--groovy-test