我想檢查一個方法未運行,並嘗試使用期望設置times = 0;
執行此操作,但是我沒有得到預期的行爲。檢查是否未調用某個方法
例如,下面的測試通過,雖然Session#stop
方法被調用,並期望具有times = 0;
條件:
public static class Session {
public void stop() {}
}
public static class Whatever {
Session s = new Session();
public synchronized void method() {
s.stop();
}
}
@Test
public void testWhatever() throws Exception {
new Expectations(Session.class) {
@Mocked Session s;
{ s.stop(); times = 0; } //Session#stop must not be called
};
final Whatever w = new Whatever();
w.method(); // this method calls Session#stop => the test should fail...
// ... but it passes
}
注意:如果我與{ s.stop(); times = 1; }
替換代碼,測試通過太:我必須缺少明顯的東西在這裏...
非常感謝 - 這很有道理。 – assylias
答案中的第一個代碼塊仍然不起作用。期望的時間= 0或1都通過!!使用'StrictExpectations'似乎正確地解決了這個問題。 –