我有一些测试方法:
@Test
public void test_method() {
MyObj mock = mock(MyObj.class);
when(mock.get("testName", "1")).thenReturn("Result1");
when(mock.get("test", "2")).thenReturn("rrrr");
}
当我尝试运行此方法时,我遇到了异常:
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.
如何模拟该方法?
答案 0 :(得分:1)
错误消息告诉您:
使用
'given().will()'
或'when().then()'
API多次重复相同的方法 请使用'will().given()'
或'doReturn().when()'
API进行存根。
答案 1 :(得分:1)
您可以使用any()模拟具有不同参数的一种方法。 any()将用作动态变量。
例如doReturn(order).when(orderRepository).save(any(Order.class));
我当时用2个不同的Order参数保存orderRepository,所以我在Order类中使用了其中任何一个。