2012-07-18 21 views
1

我在使用進行scala中的Double時遇到問題。我試圖捕捉一個嘲諷特質的論據。嘗試捕獲Int時,相同的語法正常工作。在scala中使用ArgumentCapture for Double進行ClassCastException

下面是一個例子測試:

import org.scalatest.FunSuite 
import org.scalatest.mock.MockitoSugar 
import org.mockito.Mockito._ 
import org.mockito.ArgumentCaptor 

trait MockedTrait { 
    def mockedDoubleMethod(double: Double) 
    def mockedIntegerMethod(integer: Int) 
} 

class ClassUnderTest(myTrait: MockedTrait) { 
    def methodUnderTest { 
     myTrait.mockedIntegerMethod(3) 
     myTrait.mockedDoubleMethod(5.0) 
    } 
} 

class MyTest extends FunSuite with MockitoSugar { 

    test("A basic test") { 
     val myTrait = mock[MockedTrait] 

     val classUnderTest = new ClassUnderTest(myTrait) 
     classUnderTest.methodUnderTest 

     val capturedInteger = ArgumentCaptor.forClass(classOf[Int]) 
     verify(myTrait).mockedIntegerMethod(capturedInteger.capture) 

     val capturedDouble = ArgumentCaptor.forClass(classOf[Double]) 
     verify(myTrait).mockedDoubleMethod(capturedDouble.capture) // Throws ClassCastException 
    } 

} 

我得到以下異常:

java.lang.Integer cannot be cast to java.lang.Double 
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double 
    at scala.runtime.BoxesRunTime.unboxToDouble(Unknown Source) 
    at MyTest$$anonfun$1.apply$mcV$sp(MyTest.scala:30) 
    at MyTest$$anonfun$1.apply(MyTest.scala:20) 
    at MyTest$$anonfun$1.apply(MyTest.scala:20) 
    at org.scalatest.FunSuite$$anon$1.apply(FunSuite.scala:1265) 
    at org.scalatest.Suite$class.withFixture(Suite.scala:1968) 
    at MyTest.withFixture(MyTest.scala:18) 

有什麼建議?

回答

2

我有類似的問題。這應該解決它我相信:

val capturedDouble = ArgumentCaptor.forClass(classOf[java.lang.Double])