class ExampleTest { @get:Rule val rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS) lateinit var mockedFoo: Foo lateinit var booUnderTest: Bar
@Before fun setUp() { mockedFoo = Mockito.mock(Foo::class.java) booUnderTest = Bar(mockedFoo)
}
@Test fun shouldFailWithStubbingProblem() { `when`(mockedFoo.doFoo(5)).thenReturn("five") `when`(mockedFoo.doFoo(3)).thenReturn("three") assertThat(booUnderTest.doBar(5)).isEqualTo("five times") }}
org.mockito.exceptions.misusing.PotentialStubbingProblem: Strict stubbing argument mismatch. Please check: - this invocation of 'doFoo' method: foo.doFoo(3); -> at ExampleTest2.shouldCallFoo(ExperimentsTest.kt:63) - has following stubbing(s) with different arguments: 1. foo.doFoo(5); -> at ExampleTest2.shouldCallFoo(ExperimentsTest.kt:62)Typically, stubbing argument mismatch indicates user mistake when writing tests.Mockito fails early so that you can debug potential problem easily.However, there are legit scenarios when this exception generates false negative signal: - stubbing the same method multiple times using 'given().will()' or 'when().then()' API Please use 'will().given()' or 'doReturn().when()' API for stubbing. - stubbed method is intentionally invoked with different arguments by code under test Please use default or 'silent' JUnit Rule (equivalent of Strictness.LENIENT).For more information see javadoc for PotentialStubbingProblem class.
class ExampleTest { @get:Rule val rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS) val mockedFoo: Foo = Mockito.mock(Foo::class.java) val booUnderTest: Bar = Bar(mockedFoo)
@Test fun shouldFailWithStubbingProblem() { `when`(mockedFoo.doFoo(5)).thenReturn("five") `when`(mockedFoo.doFoo(3)).thenReturn("three") assertThat(booUnderTest.doBar(5)).isEqualTo("five times") }}