我用用的Mockito scalatest。使用具有值類的匹配器時,我遇到以下問題。匹配器的Mockito,斯卡拉值類和NullPointerException異常
import org.scalatest.FlatSpec
import org.scalatest.mock.MockitoSugar
import org.mockito.BDDMockito._
import org.mockito.Matchers.any
case class FirstId(val value: String) extends AnyVal
case class SecondId(val value: String) extends AnyVal
trait MockedClass {
def someMethods(firstId: FirstId, secondId: SecondId): Int
}
class ValueClassSpec() extends FlatSpec with MockitoSugar {
val mockedClass = mock[MockedClass]
val secondId = SecondId("secondId")
"Matchers" should "work for value class" in {
// given
given(mockedClass.someMethods(any[FirstId], org.mockito.Matchers.eq(secondId))).willReturn(3)
// when
val result = mockedClass.someMethods(FirstId("firstId"), secondId)
// then
assert(result == 3)
}
}
,其結果是:
ValueClassSpec:
Matchers
- should work for value class *** FAILED ***
java.lang.NullPointerException:
at io.scalac.fow.party.ValueClassSpec$$anonfun$1.apply$mcV$sp(ValueClassSpec.scala:22)
at io.scalac.fow.party.ValueClassSpec$$anonfun$1.apply(ValueClassSpec.scala:20)
at io.scalac.fow.party.ValueClassSpec$$anonfun$1.apply(ValueClassSpec.scala:20)
at org.scalatest.Transformer$$anonfun$apply$1.apply(Transformer.scala:22)
at org.scalatest.Transformer$$anonfun$apply$1.apply(Transformer.scala:22)
at org.scalatest.OutcomeOf$class.outcomeOf(OutcomeOf.scala:85)
at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
at org.scalatest.Transformer.apply(Transformer.scala:22)
at org.scalatest.Transformer.apply(Transformer.scala:20)
at org.scalatest.FlatSpecLike$$anon$1.apply(FlatSpecLike.scala:1639)
...
我發現類似的問題(Scala Value classes and Mockito Matchers don't play together),但沒有任何意見。
是否有posibility使用匹配器的Mockito與斯卡拉值類?
庫版本:2.11.2階,1.10.8的Mockito,scalatest 2.1.6
afaik'any'和'eq'不能一起使用。 – Ashalynd 2014-12-04 08:57:30
@Ashalynd - 從[文件的Mockito(http://mockito.github.io/mockito/docs/current/org/mockito/Mockito.html):**警告在參數匹配器:** 如果您正在使用參數匹配者,所有的論據都必須由匹配者提供。 例如:(示例顯示驗證,但同樣適用於存根): 驗證(模擬).someMethod(anyInt(),anyString(),eq(「第三個參數」)); 上述//是正確的 - EQ()也是一個參數匹配 – 2014-12-04 09:07:15