Task test: Creating configurations and source sets for dummy Project


(Joel DeWitt) #1
I am a complete beginner, and created a (working) Gradle 4.1 plugin and a Task test for it.

This custom task, MyTask, is for a Java build, and has a method: myMethod(), which refers to configurations and source sets from a Java build where the plugin will be used.

In a test, I can instantiate the Task using:
Project project = ProjectBuilder.builder().build()
def myTask = project.task('myMethod', type: MyTask)

But myTask refers to JavaCompile objects like: "project.configurations.runtime", and "${project.sourceSets.main.output.classesDir}", which are not obvious to create in a dummy project with ProjectBuilder.

How can I add source sets and configurations to the "dummy" Project that are needed to run myTask.execute() in @Test myTest { } ?

(Benjamin Muschko) #2

I suggest to rather test your use case with TestKit. For a good overview on testing strategies and approaches please have a look at the Testing Gradle plugins guide.