我在使用 Spock 模拟方法时遇到问题。下面是我正在使用的代码。无需任何更改,一切正常 - 模拟实现正常工作并返回“模拟”字符串。但是,如果我使用调用检查 (3 * notifier.test()) 取消注释行,则方法 notify 的模拟实现不会调用,并且测试会失败,因为通知程序模拟返回 null。为什么会这样?
class Aaaaa extends Specification {
class Notifier {
def test() {
println("Called in impl...")
return "impl"
}
}
def "Should verify notify was called"() {
given:
Notifier notifier = Mock(Notifier)
notifier.test() >> {
println("Called in mock...")
return "mock"
}
when:
notifier.test()
notifier.test()
def result = notifier.test()
then:
// 3 * notifier.test()
result == "mock"
}
}
最佳答案
Mockito-style splitting of stubbing and mocking into two separate statements will not work
需要在定义mock方法的同一行定义调用次数
关于java - Spock 模拟方法实现在检查调用数时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60542724/