zhadoxietu
zhadoxietu

Reputation: 25

How to run test "running task" in unit test

For instance, following is gradle doc:

class GreetingTaskTest {
    @Test
    public void canAddTaskToProject() {
        Project project = ProjectBuilder.builder().build()
        def task = project.task('greeting', type: GreetingTask)
        assertTrue(task instanceof GreetingTask)
    }
}

I hope test following

class GreetingTaskTest {
    @Test
    public void canAddTaskToProject() {
        Project project = ProjectBuilder.builder().build()
        def task = project.task('greeting', type: GreetingTask) {
            methInGreetingTask()
        }
        task.run()
    }
}

My question is: how to do it?

Upvotes: 1

Views: 197

Answers (2)

Alex
Alex

Reputation: 1639

The thing with gradle tasks is that it's rather their actions that get executed. You can get those via .actions for a task and then execute each of those with .execute(task).

project.tasks.getByName("myTask").with { Task task ->
    assertEquals task.group, 'My Group'

    task.actions.each { Action action ->
        action.execute task
    }
}

Tested with gradle 6.4 / groovy 2.5.10

Upvotes: 1

Opal
Opal

Reputation: 84854

You're a looking for execute method defined in AbstractTask - gradle internales. See the docs.

Upvotes: 0

Related Questions