Spock 模拟对象可以包装 Mockito 模拟对象吗?

Can Spock mock object wrap a Mockito mock object?

我一直在使用 Spock 进行单元测试,它很棒,但我有一些创建 Mockito 模拟的遗留代码,使用此遗留代码的任何规范都必须回退到使用 Mockito 操作,例如 验证.

例如:

public class FooSpec extends Specification {

  def "services invoked when something happens"() {
    given: "legacy code that creates mock services that are Mockito mocks"
    ServiceFactory serviceFactory = new MockitoServiceFactory()

    and: "a foo instance"
    def foo = new Foo()

    and: "the foo instance is configured with a service that is a Mockito mock"
    foo.service = serviceFactory.getService("foo")

    when: "the foo instance sends an event"
    foo.sendEvent()

    then: "the foo service actually sends the event"
    verify(foo.service, times(1)).sendEvent() || true
  }
}

我更愿意以 Spock 的方式进行验证:

  1 * foo.service.sendEvent()

这是一个过于简单的示例,但是有什么方法可以包装 Mockito 模拟对象,使其表现得像 Spock 模拟对象吗?我考虑过创建一个 SpockServiceFactory 来生成 Spock 模拟对象,但我记得 Spock 不允许在规范之外创建模拟(也许这已经改变了?)

无法(不分叉 Spock 代码库)使用 Spock 的模拟语法验证 Mockito 模拟。

but I recalled that Spock does not allow mocks to be created outside of specifications (maybe this has changed?)

它还没有改变(基础 class 是目前你能得到的最接近的)。