在下面的代码中,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