So, I have a Play 2.5 project using scala, and I have these dependencies on my build.sbt:
"org.scalatestplus.play" %% "scalatestplus-play" % "1.5.1" % Test
"org.mockito" % "mockito-all" % "1.10.19" % Test
And my test looks like this:
import java.util.UUID
import org.mockito.Mockito._
import org.scalatest.BeforeAndAfter
import org.scalatest.mock.MockitoSugar
import org.scalatestplus.play.{OneAppPerSuite, PlaySpec}
import play.api.mvc.{AnyContentAsEmpty, Result}
import play.api.test.{FakeRequest, Helpers}
import play.api.test.Helpers._
import services.TargetServiceImpl
import scala.concurrent.Future
class MyControllerTest extends PlaySpec with MockitoSugar with BeforeAndAfter with OneAppPerSuite {
val targetService = mock[TargetServiceImpl]
var subject = new TargetAjaxController(targetService)
"TargetAjaxController" should {
"use the mocked service to delete a target" in {
val oauthToken = UUID.randomUUID().toString
val targetId = Random.nextLong()
val result: Future[Result] = subject.delete(targetId).apply(fakeRequestWithSession(oauthToken))
verify(targetService).deleteTarget(targetId, oauthToken)
status(result) mustBe OK
contentType(result) mustBe Some("application/json")
}
}
private def fakeRequestWithSession(oauthToken: String): FakeRequest[AnyContentAsEmpty.type] = {
FakeRequest(Helpers.DELETE, "/targets")
.withSession(
("token", oauthToken)
)
}
}
When I run this test, I get an error message saying:
org.mockito.exceptions.verification.WantedButNotInvoked: Wanted but not invoked (...) Actually, there were zero interactions with this mock
But when I put some println's within targetService#deleteTarget
, I see printed in the console that the code was actually executed as expected and the mock got invoked, Mockito just didn't record the usage of the mock.
The strangest thing is that this works fine on developers machines, the error is only raised when ran in Jenkins.
Am I doing anything wrong here? Any idea why the verify
call won't work?
Thanks a lot!