kotlin - io.mockk.MockKException : no answer found when mocking list of objects with ObjectMapper. readValue() 使用 mockk?

标签 kotlin mocking objectmapper mockk

有类似的问题,但没有专门针对 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/

相关文章:

testing - 来自同一JSON映射的Wiremock返回成功或错误响应

java - objectMapper 将印地语文本转换为特殊字符 "???"

database - 如果嵌套 var 存在于 Realm 中,在 Swift 中如何进行更新?

java - 修改json字符串中的属性值

php - 如何在 Symfony 3 中模拟 'find' 方法

amazon-web-services - 如何在golang中模拟request.Request

android - Kotlin:如何使用 Anko DSL 制作工具栏菜单?

android - 使用 Kotlin 检测 Android 内存泄漏的库

firebase - 如何从 Firebase 数据库中检索数据并将其存储在 ListArray<Class> 中? - Kotlin

kotlin - 如何在返回 Deferred<T> 的异步函数中进行延迟