0
我有以下代码:如何使用JMockit模拟接受Closure参数的Groovy方法?
def method() {
try {
dependency0.call({ arg -> })
} catch {
dependency1.call()
}
}
和下面的测试:
@Test
void shouldDoSomething(
@Mocked final Dependency0 dependency0Mock) {
final dependency1Mock = getDependency1Mock()
new Expectations() {{
dependency0Mock.call((Closure) any)
result = new Exception('expected')
final sut = new Sut(dependency1Mock, dependency0Mock)
sut.method()
}
当测试运行时,会发出以下异常:
mockit.internal.UnexpectedInvocation: Parameter "arg" of Dependency0#call(groovy.lang.Closure arg) expected null, got [email protected]
我怎么去嘲笑采用Groovy Closure的方法?
更新:即使参数类型为Integer
也会引发同样的异常。
对于以参数为参数的构造函数,这不起作用。在这种情况下,来自Groovy的模拟/存根是不够的。 – Abbadon