jeudi 15 septembre 2016

Test custom Gradle task with parameters

I have the following task with input parameters in Groovy:

class CustomTask extends DefaultTask {
    String folder = '.'
    String filePattern = /.*/

    @TaskAction
    def doSmth() {
        FileHelper help = new FileHelper()
        help.do(folder, filePattern)
    }
}

And I want to test it:

class CustomTaskTest extends GroovyTestCase {
    void testDoSmth() {
        def mock = new MockFor(FileHelper )
        mock.demand.do() { }
        mock.use {
            Project project = ProjectBuilder.builder().build()
            def task = project.task('test', type: CustomTask)
            assertTrue(task instanceof CustomTask)
            task.execute()
        }
    }
}

and test fails at run with:

org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':test'.
...
Caused by: groovy.lang.MissingMethodException: No signature of method: com.my.proj.tasks.CustomTaskTest$_testDoSmth_closure1.call() is applicable for argument types: (java.lang.String, java.lang.String) values: [., .*]

Note that I successfully test tasks without parameters and I've tried to pass closure when creating task like:

def task = project.task('test', type: CustomTask, { -> folder = ''; filePattern = ''})

but I get the same error, only values of parameters are changed.

How can fix this? Thank you.

Aucun commentaire:

Enregistrer un commentaire