How to test task classes?

What is the proper way of testing custom tasks classes?

When I try to instantiate and object with ‘new’ operator I receive the following exception:

Task of type '' has been instantiated directly which is not supported. Tasks can only be created using the DSL.

It seems to be ok. Then I create the task in the following way:

def project = ProjectBuilder.builder().build()
project.task(SampleTask.NAME, type: SampleTask)
def vst = project.tasks[SampleTask.NAME] as SampleTask

It works, but I can’t set the value of ‘vst’ private field with the following piece of code:

def confs = new TreeMap<Integer,Configuration>()
vst.configurations = confs

The field declaration is as following:

private Map<Integere, Configuration> configurations

Don’t bother with ‘@Inject’, it’s not being used while testing but works in production.

This is pretty awkward right now. The missing piece of Gradle functionality is being able to influence dependency injection while creating test instances in order to inject test doubles.

Until this feature is added, your best strategy is adding a packaged scoped setter for the field.