How to test cutom plugin with AfterEvaluate


(Thibault Kruse) #1

Hi, In a custom plugin, I create tasks in the plugin apply method like this.

@Override
    void apply( final Project project )
    {
        project.repositories.jcenter()
        project.extensions.create('foo', ...)
             // need to have extensions read
        project.afterEvaluate {
            if (project.foo.enable) {
                project.tasks.create('foo', FooTask.class)
          }
        }
    }

Now I want to write unit tests that make sure that this works as expected.

Project project = ProjectBuilder.builder().build()
        project.apply plugin: 'fooPlugin'
assert project.tasks.foo ...

This approach does not work, because the afterEvaluate closure was not executed. How can i cause that?

I want to do it like that so that users may choose to use the plugin but not necessarily use all tasks. If there is something generally wrong with my approach tell me so.


(Peter Niederwieser) #2

It might be better to have finer-grained plugins, and perhaps some convenience plugins that aggregate them. There isn’t currently an easy way to “unit” test ‘afterEvaluate’. Either you put some research into which internals to invoke (I can’t tell offhand), or you test such behavior at a higher level, e.g. by invoking a test build via the tooling API.


(Thibault Kruse) #3

In that case I guess I will rather define an extracted method that sets up the task, and invoke that in the tests manually (invoking my own internals).

Splitting the plugin is also possible in my case, but twice the hassle for documenting and releasing, so I might rather do that later.

Thanks.


(Peter Niederwieser) #4

Extracted method is a good solution.