Remove invocation from mock after it's being verified
See original GitHub issueAfter an invocation has been verified, it should be removed from the invocations list in the mock so it does not interfere with any further usage of the mock. (Since I work a lot with actors nowadays, I would expect a similar behavior here as when testing actors. After an expected message has been received by a test actor, it is also removed and any following ‘expectNoMsg()’ would then succeed. See Akka TestKit)
Case: in a test you have something like this
public void testMethod() {
SomeObject mock = mock(SomeObject.class);
// do a interaction which uses the mock
// verify interactions with the mock
verify(mock).someMethod("expected argument");
// do another invocation which does not involve the mock
// verify mock was now not involved
verify(mock, never()).someMethod(anyString()); // FAILS since it still knowns the previous, but already verified invocation.
}
The second verification fails since the mock still remembers the first invocation. Because it is already verified, it could however be removed so another verification would not be effected by it.
This is a very general case. A more specific case, where I am encountering this issue, is in a test for a Websocket manager where I want to verify the actual message send to a Websocket. In the test setup, the manager is created and a mocked Websocket is added. When a websocket is added, a ‘connection: accepted’ message is send over the websocket. The sending of this message to the mocked Websocket is verified in the setup. Then, in a test case, I send the manager an unsupported event which should not be send to the websocket, but when I now verify the websocket.send() method, it still ‘knowns’ the ‘connection: accepted’ message and the check for ‘never()’ fails.
I currently fixed it by resetting the mock after the verification in the setup, but this kinda sucks because resetting also resets the whole stubbing (see issue #183). However, when a verification would remove a verified invocation, then resetting of the invocation which issue #183 requests would no longer be needed.
Issue Analytics
- State:
- Created 8 years ago
- Comments:5 (4 by maintainers)
Top GitHub Comments
Actually this should work with
inOrder
This is because normal verification is non greedy, it feel more natural, i.e. it is possible to assert in any order. In contrast inOrder mode is greedy to ensure tests don’t pass upon a stricter ordering which is matching this case here : explanation.
OK fair enough let’s see how that pans out. It still is beta. Eventually we can annotate it with
@Incubating
.