voghDev
voghDev

Reputation: 5791

Using Mockito doAnswer in Kotlin

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

Upvotes: 21

Views: 16296

Answers (3)

Hjelmen
Hjelmen

Reputation: 91

Nowadays you could also do something like this (assuming the type is Repository):

    mock<Repository>() {
        on { getDesign(any() as GetDesign.Listener) } doAnswer {
            val callback = it.arguments[0] as GetDesign.Listener
            callback.onSuccess(Design())
            null
        }
    }

Upvotes: 0

HA S
HA S

Reputation: 1247

I am a fan of Full Mocking object, I don't want loading any configuration or any other spring boot dependency injection kicking in.

If I have to Mock a JavaMailSender function I will do it like this. I will use theAnswer to return the value.

Kotlin (Just Test JavaMailSender)

@Test
fun javaMailSenderTest(){
val jms = mock(JavaMailSender::class.java)
val mimeMessage = mock(MimeMessage::class.java)
mimeMessage.setFrom("[email protected]")
mimeMessage.setText("Just a body text")

Mockito.`when`(jms.send(mimeMessage)).thenAnswer {
  // val callback = it.arguments[0]  <- Use these expression to get params values
  // Since JavaMailSender::send() function retrun void therefore, we should return Unit
  Unit
}

assertEquals(jms.send(mimeMessage), Unit)

}

Kotlin (Using Custom Class for JavaMailSender)

chances are that you might be using your own Custom class for sending mail, so here I have done it.

@Test
fun updateMembers&SendEmailTest() {

val mockEmailService= mock(MyEmailServiceImplementor::class.java)

// sendInfoMail(true|false) is my custom implemention of JavaMailSender
Mockito.`when`(mockEmailService.sendInfoMail(true)).thenAnswer { invocation ->
  Unit
}

assertEquals(mockEmailService.sendInfoMail(true), Unit)

}

Hope this helps somebody. If you need a Java version let me know in the comments

Upvotes: 0

Anton Holovin
Anton Holovin

Reputation: 5673

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))

Upvotes: 33

Related Questions