可以模拟动态对象

时间:2019-06-12 04:13:33

标签: spring spock

如您所见,我尝试动态模拟响应。

underTest.getByTaskIds(taskIds)将传递taskIds的列表,在我将分别调用underTest.getByTaskId(taskId, channelId, false)的方法内部。 但这行不通。

    when:
    def actual = underTest.getByTaskIds(taskIds)

    then:
    taskIds.forEach({ taskId ->
        underTest.getByTaskId(taskId, channelId, false) >> mockResp.get(taskId)
    })
    actual.size() == expectedResultSize

    where:
    taskIds                                             | mockResp     | expectedResultSize
    [] as Set<UUID>                                     | [key: value] | 1
    [UUID.randomUUID()] as Set<UUID>                    | [key: value] | 1
    [UUID.randomUUID(), UUID.randomUUID()] as Set<UUID> | [key: value] | 2

示例

    class UnderTest:

        void taskId(id){
            do something
        }

        void taskIds(ids)->
        {
            this.taskId(id)
        }

0 个答案:

没有答案