Search code examples
asynchronousscalamock

Scalamock expect eventually


I'm using scalamock to write a test. The problem is that action is asynchronous. I have the following pseudo code

val resultCollectorMock = mock[ResultCollector]
(resultCollectorMock.collectResult _).expect(someResult)

val serviceUnderTest = new ServiceUnderTest(resultColletorMock)
serviceUnderTest.runAsyncJob(someParams)

This fails because the result is computed asynchronously, at time when the test ends, it's still not ready, so collectResult wasn't called, yet.

What I want to have is expectEventually(value)(patienceConfig) that is able to wait for some time for the method to be called.

I tried to use a sutb and verify instead, I wrapped it in eventually from scalatest but to no avail. For whatever reason verify seems to break the test at first evaluation.


Solution

  • you should use AsyncMockFactory with an appropriate test suite and Futures as described in the docs at https://scalamock.org/user-guide/integration/