我想检查一个方法是否没有运行并尝试使用 Expectation setting 来执行它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; }
,测试也通过了:我必须在这里遗漏一些明显的东西......