Spock中的doAnswer()等效项

时间:2018-11-06 04:27:15

标签: junit spock vert.x

我是Spock的新手,正在尝试实现以下方案:

@Test
public void asynchronousMethodTest() {
    JsonObject jsonObject = new JsonObject();
    jsonObject.put("name", "Lilly").put("city", "Glendale");
    AsyncResult<JsonObject> asyncResult = Mockito.mock(AsyncResult.class);
    when(asyncResult.succeeded()).thenReturn(true);
    when(asyncResult.result()).thenReturn(jsonObject);
    doAnswer(new Answer<AsyncResult<JsonObject>>() {
        @Override
        public AsyncResult<JsonObject> answer(InvocationOnMock invocation) throws Throwable {
            ((Handler<AsyncResult<JsonObject>>) invocation.getArguments()[1]).handle(asyncResult);
            return null;
        }
    }).when(someService).callSomeService(Mockito.any(), Mockito.any());
    childVerticle.asynchronousMethod();

    //verify(someService, times(1)).callSomeService(Mockito.any(), Mockito.any());
}

什么是spock等同于上面的代码?

1 个答案:

答案 0 :(得分:1)

是的,请查看文档的this部分。这是相关的部分:

subscriber.receive(_) >> { args -> args[0].size() > 3 ? "ok" : "fail" }

其中subscriber被定义为Mock