Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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
Java 使用mock方法参数模拟后续步骤_Java_Unit Testing_Asynchronous_Mocking_Mockito - Fatal编程技术网

Java 使用mock方法参数模拟后续步骤

Java 使用mock方法参数模拟后续步骤,java,unit-testing,asynchronous,mocking,mockito,Java,Unit Testing,Asynchronous,Mocking,Mockito,我的代码中有一个类似以下内容的方法: action.onResult(new Handler<MyClass>() { @Override public MyClass handle() { // Do something here } } } when(mockedAction.onResult(any(Handler.class))).thenReturn(firstArg.handle()); 也就是说,我想调用发送到方法onRes

我的代码中有一个类似以下内容的方法:

action.onResult(new Handler<MyClass>() {
        @Override
        public MyClass handle() { // Do something here }
     }
}
when(mockedAction.onResult(any(Handler.class))).thenReturn(firstArg.handle());
也就是说,我想调用发送到方法
onResult
的参数的
handle
方法。我无法模拟处理程序,因为它使用调用类的内部方法(我曾考虑使用私有类,但还没有找到足够好的解决方案)


动机:这是一种用于同步区域的异步回调机制。我想模拟对处理程序本身的调用,以便在代码中同步地继续流。

好的,未经测试,但在这种情况下,
ArgumentCaptor
有一种可能的用法:

final ArgumentCaptor<Handler> captor = ArgumentCaptor.forClass(Handler.class);

when(mock.onResult(captor.capture())).thenReturn(captor.getValue().handle());
final ArgumentCaptor captor=ArgumentCaptor.forClass(Handler.class);
when(mock.onResult(captor.capture())。然后return(captor.getValue().handle());

但是不确定捕获者是否有“时间”在这里初始化。

Hmm,有
ArgumentCaptor
可以这样做,但我从未尝试过这样使用它。。。我通常在
verify
上使用它来检查mock被调用的值with@fge-看起来很有趣。也许是我需要的。我正在检查它是否有效:)不得不再挣扎一点,因为
onResult
无效,我不记得如何测试无效。但这与这个问题无关,我利用你的建议设法做到了。谢谢