有类似的问题,但没有专门针对 kotlin、mockk 和使用 objectMapper.readValue 读取对象列表的问题。
给定一个方法:
fun someMethod(message: Message): List<Animal> = objectMapper.readValue(
String(message.body),
object : TypeReference<List<Animal>>() {}
)
我试着在这里模拟它:
@Test
fun `test you filthy animals`() {
...
val animals: List<Animal> = emptyList()
every { objectMapper.readValue<List<Animal>>(
any<String>(),
any<Class<List<Animal>>>()
) } returns animals
...
}
但这没有用。我收到以下错误:
io.mockk.MockKException: no answer found for: ObjectMapper(#72).readValue(
somebody,
be.kind.to.Nature$someMethod$animals$1@46b2a11a
)
暂停。
最佳答案
我花了很长时间来解决这个问题,但在这里分享它是为了繁荣!
@Test
fun `test you filthy animals`() {
...
val animals: List<Animal> = emptyList()
every { objectMapper.readValue<List<Animal>>(
any<String>(),
any<TypeReference<List<Animal>>>()
) } returns animals
...
}
关于kotlin - io.mockk.MockKException : no answer found when mocking list of objects with ObjectMapper. readValue() 使用 mockk?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63538540/