Junit Spock中的doAnswer()等价
我是斯波克的新手,正在尝试实现以下情景:Junit Spock中的doAnswer()等价,junit,spock,vert.x,Junit,Spock,Vert.x,我是斯波克的新手,正在尝试实现以下情景: @Test public void asynchronousMethodTest() { JsonObject jsonObject = new JsonObject(); jsonObject.put("name", "Lilly").put("city", "Glendale"); AsyncResult<JsonObject> asyncResult = Mockito.mock(AsyncResult.clas
@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());
}
@测试
public void asynchronousMethodTest(){
JsonObject JsonObject=新的JsonObject();
jsonObject.put(“名称”、“礼来”).put(“城市”、“格伦代尔”);
AsyncResult AsyncResult=Mockito.mock(AsyncResult.class);
当(asyncResult.succeed())。然后返回(true);
when(asyncResult.result()).thenReturn(jsonObject);
doAnswer(新答案){
@凌驾
公共异步结果应答(invocationMock调用)抛出可丢弃的{
((Handler)invocation.getArguments()[1]).handle(asyncResult);
返回null;
}
}).when(someService).callSomeService(Mockito.any(),Mockito.any());
childVerticle.asynchronousMethod();
//验证(someService,times(1)).callSomeService(Mockito.any(),Mockito.any());
}
spock与上述代码的等效性是什么?是的,这是可能的,请查看部分文档。以下是相关部分:
subscriber.receive(_) >> { args -> args[0].size() > 3 ? "ok" : "fail" }
如果
订户
被定义为模拟是的,这是可能的,请查看部分文档。以下是相关部分:
subscriber.receive(_) >> { args -> args[0].size() > 3 ? "ok" : "fail" }
其中订户
定义为一个模拟