使用Mockito验证序列调用的顺序

时间:2016-02-02 03:08:39

标签: scala mockito

我想验证序列调用的顺序,但它没有像我预期的那样工作。

import akka.actor.ActorSystem
import akka.testkit.TestKit
import org.scalatest._
import org.specs2.mock.Mockito

class Test extends TestKit(ActorSystem("testSystem"))
with WordSpecLike
with BeforeAndAfterAll
with PrivateMethodTester
with `enter code here`Mockito
{
  val m = mock[java.util.List[String]]
  m.get(0) returns "one"
  there was two(m).get(2) //do not throw any error, why???
}

我正在使用
斯卡拉2.11.7,
specs2-core 3.6.6,
specs2-mock 3.6.6,
scalatest 2.2.4

THX

1 个答案:

答案 0 :(得分:1)

我认为你不能混合使用Specs2和ScalaTest。

您移除import org.scalatest._并改为使用import org.specs2.mutable.SpecificationLike

import akka.testkit.TestKit
import akka.actor.ActorSystem
import org.specs2.mock.Mockito
import org.specs2.mutable.SpecificationLike

class Test extends TestKit(ActorSystem("testSystem"))
  with Mockito
  with SpecificationLike
{
  "it" should{
    "raise error" in {
      val m = mock[java.util.List[String]]
      m.get(0) returns "one"
      there was two(m).get(2)
    }
  }
}

现在您可以看到sbt test返回类似的内容。

[error]    The mock was not called as expected:
[error]    Wanted but not invoked:
[error]    list.get(2);
[error]    -> at Test$$anonfun$1$$anonfun$apply$1$$anonfun$apply$3.apply(Test.scala:14)
[error]    Actually, there were zero interactions with this mock. (Test.scala:14)