what would be the Kotlin equivalent to this Java code?
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Design design = new Design();
GetDesign.Listener callback = (GetDesign.Listener) invocation.getArguments()[0];
callback.onSuccess(design);
return null;
}
}).when(someRepository).getDesign(any(GetDesign.Listener.class));
[更新]在try 了几个选项之后,我终于用mockito-kotlin解决了这个问题.我认为这是实现doAnswer
的最舒适的方式.语法几乎保持不变:
doAnswer {
callback = it.arguments[0] as GetDesign.Listener
callback.onSuccess(Design())
null
}.whenever(someRepository).execute(any(GetDesign.Listener::class.java))
Complete code and build.gradle configuration can be found here