unit-testing - 我可以在 Kotlin 中使用 MockK 模拟通用(模板)私有(private)方法吗?

标签 unit-testing kotlin mocking mockk

我想模拟以下功能:

private fun <T> updateItemInDb(id: Long, column: String, data: T)

我的类通过以下方式调用它:

updateItemInDb(it, DB_POS, i) , 其中itLong , DB_POSStringi是一个 Int .

我希望函数不做任何事情就直接运行。我在单元测试中尝试了以下内容:

1) every { adapter["updateItemInDb"](any<Long>(), any<String>(), any<Int>()) } just Runs

这给了我一个类型不匹配错误:required MockKStubScope<Unit>, found MockKStubScope<Any?>

2) every { adapter["updateItemInDb"](any<Long>(), any<String>(), any<Int>()) } answers { }

这在运行时失败了 io.mockk.MockKException: can't find function updateItemInDb(-1078155520644112829, -d008fa83c4f49c0, 843241211) for dynamic call

最佳答案

现在是的。自 1.7.16 起修复了通用私有(private)函数

关于unit-testing - 我可以在 Kotlin 中使用 MockK 模拟通用(模板)私有(private)方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50000194/

相关文章:

unit-testing - 用Spock检查基数时调用太少0

android - Kotlin 的 "@Parcelize"是否适用于非数据类?

android - 在 Kotlin Lambda 中调用 RxJava Single

android - Gson在条件下忽略序列化字段

python - 使用 Python Mock 库监视内部方法调用

javascript - "clock"指令的 Angular 单元测试 $interval

java - 使用 junit 测试 Java 中的无修饰符方法

python - 无法理解如何使用unittest模块在python中编写工作日的测试用例

python - 如何为 Python __del__ 函数编写单元测试用例

python - 使用模拟测试 python 中的函数