我有一些測試方法:@Testpublic 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.我如何模擬這個方法?
3 回答

當年話下
TA貢獻1890條經驗 獲得超9個贊
錯誤消息告訴您:
'given().will()'
使用或API多次存根同一方法'when().then()'
請使用'will().given()'
或'doReturn().when()'
API 進行存根。

慕蓋茨4494581
TA貢獻1850條經驗 獲得超11個贊
您可以使用any()來模擬具有不同參數的一種方法。any() 將用作動態變量。
例如 doReturn(order).when(orderRepository).save(any(Order.class));
我使用 2 個不同的 Order 參數保存 orderRepository,因此我將任何一個與 Order 類一起使用。

侃侃爾雅
TA貢獻1801條經驗 獲得超16個贊
對我來說,添加注釋 @MockitoSettings(strictness = Strictness.LENIENT)
@ExtendWith(MockitoExtension::class) @MockitoSettings(strictness = Strictness.LENIENT)
添加回答
舉報
0/150
提交
取消