我有一些测试方法:
@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.我怎么能模仿这个方法呢?
发布于 2019-09-17 12:54:36
错误消息告诉您:
使用
'given().will()'或'when().then()'API多次使用相同的方法,请使用'will().given()'或'doReturn().when()'API进行固执。
发布于 2020-11-20 07:43:26
您可以使用任何()来模拟一个具有不同参数的方法。any()将用作动态变量。
例如doReturn(order).when(orderRepository).save(any(Order.class));
我用两个不同的订单参数保存orderRepository,所以我使用了any with类。
https://stackoverflow.com/questions/57974644
复制相似问题