所以,我有一个使用scala的Play 2.5项目,我有这些依赖于我的build.sbt:
"org.scalatestplus.play" %% "scalatestplus-play" % "1.5.1" % Test
"org.mockito" % "mockito-all" % "1.10.19" % Test
我的测试是这样的:
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)
)
}
}
当我运行这个测试时,我得到一个错误消息说:
org.mockito.exceptions.verification.WantedButNotInvoked: Wanted but not invoked (...) Actually, there were zero interactions with this mock
但是当我在targetService#deleteTarget
中放入一些println时,我看到在控制台上打印的代码实际上按预期执行并且调用了mock, Mockito只是没有记录mock的使用情况。
最奇怪的是,这在开发机器上工作得很好,只有在Jenkins中运行时才会引发错误。
我做错了什么吗?知道为什么verify
呼叫不起作用吗?
非常感谢!
使用await before subject.delete。UnitSpec有那个方法。检查你的规格是否有