io.mockk.MockKException:使用对象模拟使用ObjectMapper.readValue模拟对象列表时找不到答案?

问题描述

存在类似的问题,但没有一个专门针对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
    ...
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...