I want to check that a method is not run and tried to do it with an Expectation setting times = 0;
, however I don't get the expected behaviour.
For example, the following test passes, although the Session#stop
method is called, and the expectation has a times = 0;
condition:
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
}
Note: If I replace the code with { s.stop(); times = 1; }
, the test passes too: I must be missing something obvious here...
Mockito verify() simple example It's the same as calling with times(1) argument with verify method. verify(mockList, times(1)). size(); If we want to make sure a method is called but we don't care about the argument, then we can use ArgumentMatchers with verify method.
verify supports the same argument matchers as every , along with a few additional matchers. Inside the verification block (between the opening curly bracket { and closing curly bracket } ), you write the method you want to verify.
public static void verifyNoMoreInteractions(Object... mocks) Checks if any of given mocks has any unverified interaction. We can use this method after calling verify() methods. It is to make sure that no interaction is left for verification.
The reason of the unexpected mocking behavior is that you inadvertently used partial mocking on an strictly mocked type. In this case, recording an expectation with times = <n>
means that the first n
matching invocations will be mocked, and after that any additional invocations will execute the original "unmocked" method. With regular mocking instead, you would get the expected behavior (ie, an UnexpectedInvocation
getting thrown after n
invocations).
The proper way to write the test is:
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 with a verification block instead, which is usually better for situations like these:
@Test
public void testWhatever (@Mocked final Session s)
{
final Whatever w = new Whatever();
w.method();
new Verifications() {{ s.stop(); times = 0; }};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With