How to test cutom plugin with AfterEvaluate

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

    void apply( final Project project )
        project.extensions.create('foo', ...)
             // need to have extensions read
        project.afterEvaluate {
            if ( {
                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 ...

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.

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.

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.


Extracted method is a good solution.