我的代码中有一个看起来像这样的方法:
action.onResult(new Handler<MyClass>() {
@Override
public MyClass handle() { // Do something here }
}
}
我希望能够模拟它(使用Mockito)。像这样的东西:
when(mockedAction.onResult(any(Handler.class))).thenReturn(firstArg.handle());
意思是,我想调用handle
发送给method的参数的method onResult
。我无法嘲笑处理程序,因为它使用了调用类的内部方法(我考虑过使用私有类,但尚未达到足够好的解决方案)
动机:这是一种异步回调机制,用于同步区域。我想模拟对处理程序本身的调用,以便在代码中同步继续执行流程。
好的,未测试,但是在ArgumentCaptor
这种情况下可以使用以下方法:
final ArgumentCaptor<Handler> captor = ArgumentCaptor.forClass(Handler.class);
when(mock.onResult(captor.capture())).thenReturn(captor.getValue().handle());
但是,不确定捕获者是否具有在此处初始化的“时间”。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句