2016-02-02 59 views
1

我想驗證序列調用的順序,但沒有按預期工作。使用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核心3.6.6,
specs2-模擬3.6.6,
scalatest 2.2.4

THX

回答

1

我不認爲你可以混合Specs2和ScalaTest。

You shuld刪除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) 
+0

這是工作,謝謝 – myregister0618