5

There was a similar question asked about Mockito here

I have a situation where I would like to mock out readValue in the following line

 val animal: Animal = objectMapper.readValue(String(message.body))

I tried

@Test
fun `test you filthy animal`() {
    val animal = Animal("bird")

    every {
        objectMapper.readValue(any<String>())
    } returns animal
}

...but I keep getting the following error:

Not enough information to infer type variable T

I have been scratching my head trying to figure it out in Mockk.

Naruto Sempai
  • 6,233
  • 8
  • 35
  • 51

1 Answers1

8

I ended up figuring it out:

import org.junit.jupiter.api.Test

@Test
fun `test you filthy animal`() {
    val animal = Animal("bird")

    every {
        objectMapper.readValue<Animal>(any<String>())
    } returns animal
}

Edit: Later I ran into more issues that were resolved with the following:

every { 
   objectMapper.readValue(any<String>(), any<TypeReference<Animal>>()) 
} returns animal
Naruto Sempai
  • 6,233
  • 8
  • 35
  • 51
  • Thanks! that was helpful for me but it didn't work in all cases. I found kotlin's jacksonObjectMapper() method which gives an object mapper for testing. In that way, doesn't need to handle every case by 'every' but give exact payload for parsing – farukcankaya Aug 24 '23 at 16:58