【发布时间】:2017-10-24 14:13:45
【问题描述】:
我想用 groovy 和 spock 来测试这个类:
class TaskRunner {
private int threads
private ExecutorService executorService
private TaskFactory taskFactory
// relevant constructor
void update(Iterable<SomeData> dataToUpdate) {
Iterable<List<SomeData>> partitions = partition(dataToUpdate, THREADS)
partitions.each {
executorService.execute(taskFactory.createTask(it))
}
}
}
我想写一个像这样的测试:
class TaskRunnerSpec extends specification {
ExecutorService executorService = Mock(ExecutorService)
TaskFactory taskFactory = Mock(TaskFactory)
@Subject TaskRunner taskRunner = new TaskRunner(taskFactory, executorService)
def "should run tasks partitioned by ${threads} value"(int threads) {
given:
taskRunner.threads = threads
where:
threads | _
1 | _
3 | _
5 | _
when:
tasksRunner.update(someDataToUpdate())
then:
// how to test number of invocations on mocks?
}
}
我看到文档中的示例仅包含 given、when、then 部分的交互测试,以及只有两个部分的数据驱动测试示例:expect 和 where。
我可以把这两个结合起来吗?或者如何实现相同的功能?
【问题讨论】:
标签: unit-testing groovy spock