我有一个A
像下面这样的类:
class A(b: B) : B by b // Note the implementation of interface B by delegation to parameter b
interface B {
fun memberFunction() {}
}
fun B.extensionFunction() {}
我现在想验证B#extensionFunction
正在调用。我怎么做?
我可以B#memberFunction
这样验证:
class BImpl : B // Dummy implementation needed to instantiate interface B
@Test
fun `memberFunction can be verified`() {
val bSpy = spyk<B>(BImpl)
val a = A(bSpy)
a.memberFunction()
verify { bSpy.memberFunction() }
}
但是,相同的测试B#extensionFunction
并不能解决问题。相反,我得到一个java.lang.AssertionError
. 验证失败,因为调用没有发生。(请注意,您必须使用静态模拟(使用mockkStatic
函数)才能验证扩展函数。)
作为参考,使用的库:
- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.11
- junit:junit:4.12
- io.mockk:mockk:1.9