我在下面的表格上有一些代码:
@Language("SQL")
val someSql = """
SELECT foo
FROM bar
WHERE foo = :foo
"""
return session.select(some, mapOf("foo" to foo)) {
MyObject(
foo = it.string("foo"),
)
}.firstOrNull()
使用下面的
com.github.andrewoma.kwery.core网站
. 注意方法签名中的lambda:
fun <R> select(@Language("SQL") sql: String,
parameters: Map<String, Any?> = mapOf(),
options: StatementOptions = defaultOptions,
mapper: (Row) -> R): List<R>
我使用Mockitokotlin2。
当使用select查询(包含“select foo”)调用session select方法时,我需要返回myObject的实例。
我在想我可以将一个模拟传递到lambda中,如下所示(但随后它将与我尝试模拟的方法调用不匹配)。下面的代码是一个尝试。但从来都不符合
eq(function2)
:
val function2: (Row) -> Int = mock {
onGeneric { invoke(any()) }.thenReturn(MyObject(foo="test-foo"))
}
val session = mock<Session> {
on { select(sql = any(), parameters = any(), options = any(), mapper = eq(function2))}.thenReturn(listOf(MyObject(foo="test-foo")))
}
在我的例子中,函数2不是一个真正的映射器,它不是我要模拟的东西的均衡器,它从不匹配,模拟也从不被调用。
那我拿什么来开玩笑呢
session, select
而不是
情商(函数2)
在上面的代码中获得myObject对象返回?