Testing Gradle task that uses javaexec()

Viewed 12

Suppose I have a custom Gradle task that uses ExecOperations.javaexec() in its code:

public class MyTask extends DefaultTask {
    private final ExecOperations execOps
    
    @Inject
    public MyTask(ExecOperations execOps) {
       this.execOps = execOps;
    }
    
    @TaskAction
    public void run() {
        ExecResult result = execOps.javaexec(spec -> {
            // classpath is set to some jar with a main class
            spec.classpath(...);
            spec.args(...);
        });
        // Do more stuff with result
    }
}

The issue I have is that I am not sure how I can create a Spock test fixture for it to validate that the jar is being run with the correct arguments without actually running the jar itself. (The jar makes remote calls, for example, which I want to avoid).

It seems like the only plausible way forward is to potentially find a way to expose the ExecOperations for testing and possibly replace it with a stub, along with turning the Action<JavaExecSpec> from a mere lambda to something more substantial. Any ideas would be appreciated.

0 Answers
Related