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));
[UPDATE] After trying several options, I finally made it work using mockito-kotlin. I think that's the most comfortable way of implementing doAnswer
. Syntax remains almost the same:
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
doAnswer {
val design = Design()
val callback = it.arguments[0] as GetDesign.Listener
callback.onSuccess(design)
null // or you can type return@doAnswer null
}.`when`(someRepository).getDesign(any(GetDesign.Listener::class.java))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With