我有一些測(cè)試方法:@Testpublic void test_method() { MyObj mock = mock(MyObj.class); when(mock.get("testName", "1")).thenReturn("Result1"); when(mock.get("test", "2")).thenReturn("rrrr");}當(dāng)我嘗試運(yùn)行這個(gè)方法時(shí),我遇到了異常: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.我如何模擬這個(gè)方法?
3 回答

當(dāng)年話(huà)下
TA貢獻(xiàn)1890條經(jīng)驗(yàn) 獲得超9個(gè)贊
錯(cuò)誤消息告訴您:
'given().will()'
使用或API多次存根同一方法'when().then()'
請(qǐng)使用'will().given()'
或'doReturn().when()'
API 進(jìn)行存根。

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

侃侃爾雅
TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超16個(gè)贊
對(duì)我來(lái)說(shuō),添加注釋 @MockitoSettings(strictness = Strictness.LENIENT)
@ExtendWith(MockitoExtension::class) @MockitoSettings(strictness = Strictness.LENIENT)
添加回答
舉報(bào)
0/150
提交
取消