In below code, Mockito verify doesn't works as expected on scala methods with default parameter but works fine on methods with no default parameters.
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 )
}
}
Please suggest, what i am doing wrong in the second test.
Edit 1: @Som Please find below stacktrace for above test class :-
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 ***
Edit 2 : @Mifeet
As suggested, if i pass 0 for default int parameter test passes, but below test case is not passing with the suggested aprroach :-
"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 )
}
Please find below stacktrace :-
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)
...
Your opinion on other existing mocking libraries like PowerMock, ScalaMock is highly appreciated, if they can provide a neat solution for such cases, as i am open to use any mocking library in my project.