【发布时间】:2020-03-05 10:04:14
【问题描述】:
我在使用 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"
}
}
【问题讨论】:
标签: java unit-testing groovy mocking spock