Executing Groovy Test Scripts Dynamically from Java: A Practical Guide

This article explains how to build a Java wrapper that loads and runs Groovy test scripts at runtime, covering classloader nuances, reflection-like execution, file discovery, method filtering, and provides a complete code example for a custom test framework.

FunTester
FunTester
FunTester
Executing Groovy Test Scripts Dynamically from Java: A Practical Guide

Purpose

This component enables a Java‑based HTTP client test framework to execute test cases written in Groovy. It loads a Groovy source file, creates an instance of the generated class, and invokes selected methods without relying on external build tools.

Design constraints

GroovyClassLoader must be instantiated as a non‑static object; a static loader would share state across executions and break isolation.

File discovery is limited to a single directory (non‑recursive) – recursion can be added later if required.

Core class: ExcuteGroovy

Located in com.fission.source.source, this class extends an abstract SourceCode base and encapsulates all responsibilities for script handling.

Member fields

private String path;

– absolute path of the Groovy script. private String name; – method name to invoke when excuteMethodByName is used. private String[] args; – placeholder for future argument support. private List<String> files = new ArrayList<>(); – collection of discovered .groovy files.

private GroovyClassLoader loader = new GroovyClassLoader(getClass().getClassLoader());

– non‑static loader that uses the current class loader as parent. private GroovyObject groovyObject; – runtime instance of the loaded Groovy class. private Class<?> groovyClass; – the compiled Groovy class.

Construction

The constructor stores path and name, then immediately calls getGroovyObject() to compile the script and instantiate the object.

Primary operations

excuteAllMethod(String path) – discovers all .groovy files under path (non‑recursive) and invokes excuteMethodByPath on each file.

excuteMethodByName() – validates that path points to a file, then calls groovyObject.invokeMethod(name, null). Errors are caught and logged.

excuteMethodByPath() – iterates over groovyClass.getDeclaredMethods(), filters methods whose name contains "test" or equals "main", and invokes them via groovyObject.invokeMethod.

Supporting methods

getGroovyObject()

– parses the Groovy file with loader.parseClass, then creates a new instance via groovyClass.newInstance(). IO, instantiation, and illegal‑access exceptions are logged. getAllFile(String path) – if path is a file, adds it to files; otherwise lists directory entries, adds those ending with .groovy, and returns the list.

Usage example

String scriptPath = "/opt/tests/sample.groovy";
String methodName = "testLogin";
ExcuteGroovy executor = new ExcuteGroovy(scriptPath, methodName);
// Execute only the method named "testLogin"
executor.excuteMethodByName();

// Execute all test‑like methods in the script
executor.excuteMethodByPath();

// Execute every Groovy file in a directory
executor.excuteAllMethod("/opt/tests");

Important notes

The loader is created per ExcuteGroovy instance, ensuring isolation between different test scripts.

Method filtering is deliberately simple (contains "test" or equals "main"); more sophisticated patterns can be added as needed.

Recursive directory traversal is not implemented; extending getAllFile with a depth‑first search would enable full test suite discovery.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaReflectiontest automationclassloaderGroovyscript execution
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.