verify number of invocations with `times` is broken
CalumMcCall opened this issue · 2 comments
Hi,
I've been trying to verify the number of times a mock is called, but my check always succeeds. My code is below, I've mimicked the functionality manually with an AtomicInteger
which works, but using times doesn't. So the test below passes, but I'd expect the line with verify()
to fail. Any idea what's going wrong here?
"retry when sendRequest returns a failed future" in {
val uriPath = "/zero/retries"
val req = mkHttpRequest(uriPath)
val config = configNoRetries.copy(retries = 3)
val mockHttpClient = mock[HttpClient]
// use AtomicInteger to count invocations, because scalatest's method of doing this using `times()` is broken
val invocationCount = new AtomicInteger(0)
(mockHttpClient
.sendRequest(_: HttpRequest)(_: ActorSystem, _: ExecutionContext))
.expects(req, *, *)
.onCall { _ =>
invocationCount.incrementAndGet()
Future.failed(new Throwable("client timed out"))
}
.anyNumberOfTimes()
val client = new RetryableFranzClient(config, mockHttpClient)
val response = client.sendRequest(req)
whenReady(response.failed) { _ =>
assert(invocationCount.get == 4, "http call must be invoked 4 times")
verify(mockHttpClient, times(2)).sendRequest(_: HttpRequest)(_: ActorSystem, _: ExecutionContext)
}
I've run into this problem before, I'm using v1.17.0, scala v2.13.8. My test class is inheriting from MockFactory
in case that's important.
Hey, there're some API bits in your example I don't recognise, are you sure you're using mockito-scala?
I mean, we don't have a method called anyNumberOfTimes
nor a trait called MockFactory
...
MockFactory
is from scalatest. I guess maybe the issue is I'm mixing test libraries? I think my issues is with ScalaMock. My apologies for the mistake!