question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Remove invocation from mock after it's being verified

See original GitHub issue

After 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:closed
  • Created 8 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
bric3commented, Aug 19, 2015

Actually this should work with inOrder

@Test
public void testMethod() {
    SomeObject mock = mock(SomeObject.class);

    mock.someMethod("expected argument");

    InOrder inOrder = inOrder(mock);
    inOrder.verify(mock).someMethod("expected argument");
    // no operations
    inOrder.verify(mock, never()).someMethod(anyString());
}

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.

0reactions
bric3commented, Dec 9, 2015

OK fair enough let’s see how that pans out. It still is beta. Eventually we can annotate it with @Incubating.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mockito verify the last call on a mocked object - Stack Overflow
By default, Mockito.verify() doesn't matter of the invocation order. To take it into consideration, wrap the mock in an InOrder instance and ...
Read more >
Re: Verifying ordered invocation of methods inside same mock
Am using v1.8.5 and this is the exception ST that am getting org.mockito.exceptions.verification.VerificationInOrderFailure: Verification in order failure
Read more >
Mockito (Mockito 4.9.0 API) - Javadoc.io
The Mockito library enables mock creation, verification and stubbing. ... Verifying exact number of invocations / at least once / never
Read more >
A Unit Testing Practitioner's Guide to Everyday Mockito - Toptal
In this article, we'll cover creating and configuring mocks and using them to verify the expected behavior of the system being tested. We'll...
Read more >
Tutorial - Mocking - JMockit
Mocking provides a mechanism for isolating a class to be tested from ... test verification, after the invocations under test had a chance...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found