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.

Mockito.verifyCompletion for spies in an asynchronous environment.

See original GitHub issue

I’m writing a component test (not a unit test) against some code which is doing some asynchronous processing. In the test, I want to wait until a function is called, before checking that the state it creates is created correctly. Currently it is possible to wait until a spy is called, but it is not possible to wait until that call completes.

This is not a bug. I already have a workaround, so it’s a low-priority feature wish:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT
        classes = {TestConfiguration.class})
public class TestPublish {
  //...
  @SpyBean PublisherService spy;

  @Test
  public void shouldRetryBeatPublishIfFirstAttemptFails() throws InterruptedException {
    //...
    Mockito.verify(spy, Mockito.timeout(10_000).atLeast(3)).checkForPublish(eq(expected));

    //Check that state created is correct.
  }
}

One possible api would be to add a Mockito.verifyCompletion Another possible api would be to add a Mockito.timeoutToCompletion

Anyways: I’ve been quite happy with being able to do timeout at all, and spies are basically heaven sent, so thank you.

Best Regards, Myrle

Edit by Mockito dev: put Java code in a snippet for syntax highlighting

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:14
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
AlexeiZenincommented, Apr 4, 2020

Definitely would be useful to have this part of the Mockito library as a timeoutTillCompletion() style method with verify(). Please review this again and look at HadarLevi’s push above.

3reactions
lhannestcommented, Nov 25, 2020

Bump for this issue. I’ve found the same workaround, with a utility method:

public static org.mockito.stubbing.Stubber countDown(CountDownLatch latch) {
    return org.mockito.Mockito.doAnswer(i -> {
        try {
            return i.callRealMethod();
        } finally {
            latch.countDown();
        }
    });
}
CountDownLatch latch = new CountDownLatch(1);

countDown(latch).when(mock).methodCall();

latch.await();

But a one-line Mockito.verifyCompletion with timeout parameters would be much more attractive.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mockito - Mock a method inside async method that is under test
Hi I am trying to do some unit tests with async methods. below is my code. the first line of ...
Read more >
Mockito (Mockito 4.9.0 API) - Javadoc.io
The Mockito library enables mock creation, verification and stubbing. ... Spying or mocking abstract classes (Since 1.10.12, further enhanced in 2.7.13 and ...
Read more >
Unit testing asynchronous methods with Mockito.
We have 2 options to test our asynchronous method but first we will create our test class DummyCollaboratorCallerTest (for convention we just ...
Read more >
Mock Method To Return Different Values in Mockito - TURRETA
Verify Mock Method Returns Different Values ... Finally, we assert that the combination of stubs and arguments should throw RuntimeException with ...
Read more >
Best Practices for Spies, Stubs and Mocks in Sinon.js
Together, spies, stubs and mocks are known as test doubles. Similar to how stunt doubles do the dangerous work in movies, we use...
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