Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock one method with different parameters multiple times?

Tags:

java

mockito

I have some testing method:

@Test
public void test_method() {
    MyObj mock = mock(MyObj.class);
    when(mock.get("testName", "1")).thenReturn("Result1");
    when(mock.get("test", "2")).thenReturn("rrrr");
}

when I trying run this method I had exception:

org.mockito.exceptions.misusing.PotentialStubbingProblem: 
Strict stubbing argument mismatch. Please check:

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.

How can I mock this method?

like image 742
John Avatar asked Sep 16 '25 09:09

John


1 Answers

The error message tells you:

stubbing the same method multiple times using 'given().will()' or 'when().then()' API Please use 'will().given()' or 'doReturn().when()' API for stubbing.