Using GradleRunner to test a Custom Task


(Stefan Ziegler) #1

Hi

I have some trouble to test a custom task with Gradle TestKit and Spock.

According the documentation the classpath of the test process can be injected with the Java Gradle Plugin Development Plugin. But it does seem to work for plugins only and not with custom task.

I need to import the custom task with an import statement:

        buildFile << """
            import org.catais.dmt.task.validation.IlivalidatorTask
            task validateFile(type: IlivalidatorTask) {
                fileName = "${xtfFileName}"
                xtflog = true
            }
        """

But this ends with a “unable to resolve class” error. The only thing that helps is the approach for Gradle versions prior to 2.8: https://docs.gradle.org/current/userguide/test_kit.html#sec:working_with_gradle_versions_prior_to_28

I’m a bit confused if it’s the right approach of if I’m missing something. Should the Java Gradle Plugin Development Plugin not also work with Custom Tasks? Any hints or ideas?

best regards
Stefan


(Benjamin Muschko) #2

Short answer: Java Gradle Plugin Development Plugin needs a plugin to properly configure the classpath. If you want to just test a custom task then you’d need to resort to the configuration of TestKit via buildscript. Unfortunately, that’s a bit inconvenient. I’d suggest you open an issue on GitHub if you are interested in making it more convenient.