在下面的代碼中,Mockito驗證不按預期方式在默認參數的scala方法上正常工作,但可以在沒有默認參數的方法上正常工作。Mockito驗證失敗,並在Scala中使用默認參數「TooManyActualInvocations」
package verifyMethods
import org.junit.runner.RunWith
import org.mockito.Mockito
import org.mockito.Mockito.times
import org.scalatest.FlatSpec
import org.scalatest.Matchers.be
import org.scalatest.Matchers.convertToAnyShouldWrapper
import org.scalatest.junit.JUnitRunner
import org.scalatest.mock.MockitoSugar
trait SUT {
def someMethod(bool: Boolean): Int = if (bool) 4 else 5
def someMethodWithDefaultParameter(bool: Boolean, i: Int = 5): Int = if (bool) 4 else i
}
@RunWith(classOf[JUnitRunner])
class VerifyMethodWithDefaultParameter extends FlatSpec with MockitoSugar with SUT {
"mockito verify method" should "pass" in {
val sutMock = mock[SUT]
Mockito.when(sutMock.someMethod(true)).thenReturn(4, 6)
val result1 = sutMock.someMethod(true)
result1 should be(4)
val result2 = sutMock.someMethod(true)
result2 should be(6)
Mockito.verify(sutMock, times(2)).someMethod(true)
}
//this test fails with assertion error
"mockito verify method with default parameter" should "pass" in {
val sutMock = mock[SUT]
Mockito.when(sutMock.someMethodWithDefaultParameter(true)).thenReturn(4, 6)
val result1 = sutMock.someMethodWithDefaultParameter(true)
result1 should be(4)
val result2 = sutMock.someMethodWithDefaultParameter(true)
result2 should be(6)
Mockito.verify(sutMock, times(2)).someMethodWithDefaultParameter(true)
}
}
請建議,我在做第二次測試中的錯誤。
編輯1: @Som 請看以下堆棧跟蹤上面測試類: -
Run starting. Expected test count is: 2
VerifyMethodWithDefaultParameter:
mockito verify method
- should pass
mockito verify method with default parameter
- should pass *** FAILED ***
org.mockito.exceptions.verification.TooManyActualInvocations: sUT.someMethodWithDefaultParameter$default$2();
Wanted 2 times:
-> at zeither.VerifyMethodWithDefaultParameter$$anonfun$2.apply$mcV$sp(VerifyMethodWithDefaultParameter.scala:37)
But was 3 times. Undesired invocation:
-> at zeither.VerifyMethodWithDefaultParameter$$anonfun$2.apply$mcV$sp(VerifyMethodWithDefaultParameter.scala:34)
...
Run completed in 414 milliseconds.
Total number of tests run: 2
Suites: completed 1, aborted 0
Tests: succeeded 1, failed 1, canceled 0, ignored 0, pending 0
*** 1 TEST FAILED ***
編輯2:@Mifeet
至於建議,如果我爲默認的int參數測試通過傳遞0,但在測試用例下面沒有傳遞給建議的aprr oach: -
"mockito verify method with default parameter" should "pass" in {
val sutMock = mock[SUT]
Mockito.when(sutMock.someMethodWithDefaultParameter(true, 0)).thenReturn(14)
Mockito.when(sutMock.someMethodWithDefaultParameter(false, 0)).thenReturn(16)
val result1 = sutMock.someMethodWithDefaultParameter(true)
result1 should be(14)
val result2 = sutMock.someMethodWithDefaultParameter(false)
result2 should be(16)
Mockito.verify(sutMock, times(1)).someMethodWithDefaultParameter(true)
Mockito.verify(sutMock, times(1)).someMethodWithDefaultParameter(false)
}
請看以下堆棧跟蹤: -
mockito verify method with default parameter
- should pass *** FAILED ***
org.mockito.exceptions.verification.TooManyActualInvocations: sUT.someMethodWithDefaultParameter$default$2();
Wanted 1 time:
-> at zeither.VerifyMethodWithDefaultParameter$$anonfun$2.apply$mcV$sp(VerifyMethodWithDefaultParameter.scala:38)
But was 2 times. Undesired invocation:
-> at zeither.VerifyMethodWithDefaultParameter$$anonfun$2.apply$mcV$sp(VerifyMethodWithDefaultParameter.scala:35)
...
你像PowerMock其他現有的嘲諷圖書館的意見,ScalaMock的高度讚賞,如果能提供這樣的情況下很好地解決,如我願意在我的項目中使用任何模擬庫。
請一定要清楚你「期望」和你看到什麼。 –
查看我的更新回答。有時候你只需要在彎曲的框架中淹沒他們沒有設計的東西之前弄髒自己的手。 – Mifeet