1
當使用MockFor時,我如何才能驗證n方法是否被調用至少?我試着忽略設立後需求的方法調用,就像這樣:使用MockFor,如何驗證一個方法被調用至少n次?
import groovy.mock.interceptor.MockFor;
import org.junit.Test
class FilterTest {
interface Filter {
boolean isEnabled()
}
@Test
public void test() {
MockFor mockContext = new MockFor(Filter)
// Expect at least one call
mockContext.demand.isEnabled {true}
mockContext.ignore.isEnabled {false}
// Obtaining a usuable mock instance
def filter = mockContext.proxyInstance()
// Fake calling the method
filter.isEnabled()
filter.isEnabled()
// Verify invoked at least once?
mockContext.verify(filter)
}
}
不過,我得到一個斷言失敗:
junit.framework.AssertionFailedError: verify[0]: expected 1..1 call(s) to
'isEnabled' but was called 0 time(s).
謝謝,那正是我之後的! – Klarth 2013-02-14 10:58:20