The reason for the unexpected mocking behavior is that you accidentally used a partial mockery of a severely mocking type. In this case, recording the wait with times = <n> means that the first n matching calls will be mocked, and after that any additional calls will be made by the original "unmocked" method. With the usual mockery, you will get the expected behavior instead (i.e., UnexpectedInvocation obtained after n invocations).
The correct way to record a test:
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 () { new Expectations() { @Mocked Session s; { s.stop(); times = 0; } }; final Whatever w = new Whatever(); w.method(); }
Alternatively, it can also be written using a validation block, which is usually better suited for such situations:
@Test public void testWhatever (@Mocked final Session s) { final Whatever w = new Whatever(); w.method(); new Verifications() {{ s.stop(); times = 0; }}; }
source share