Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Junit Spock中的doAnswer()等价_Junit_Spock_Vert.x - Fatal编程技术网

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" }
其中
订户
定义为一个
模拟