所以,我有一个使用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时,我看到在控制台中打印出代码实际按预期执行并且模拟被调用,Mockito只是没有记录模拟的用法。
最奇怪的是,这在开发人员机器上工作正常,只有在Jenkins中运行时才会出错。
我在这里做错了吗?知道为什么verify
电话不起作用吗?
非常感谢!
答案 0 :(得分:0)
在subject.delete之前使用await。 UnitSpec有这种方法。检查您的规格是否可用。