亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何多次模擬具有不同參數的一種方法?

如何多次模擬具有不同參數的一種方法?

慕絲7291255 2023-08-04 16:44:33
我有一些測試方法:@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 進行存根。


查看完整回答
反對 回復 2023-08-04
?
慕蓋茨4494581

TA貢獻1850條經驗 獲得超11個贊

您可以使用any()來模擬具有不同參數的一種方法。any() 將用作動態變量。


例如 doReturn(order).when(orderRepository).save(any(Order.class));


我使用 2 個不同的 Order 參數保存 orderRepository,因此我將任何一個與 Order 類一起使用。


查看完整回答
反對 回復 2023-08-04
?
侃侃爾雅

TA貢獻1801條經驗 獲得超16個贊

對我來說,添加注釋 @MockitoSettings(strictness = Strictness.LENIENT)

@ExtendWith(MockitoExtension::class)
@MockitoSettings(strictness = Strictness.LENIENT)


查看完整回答
反對 回復 2023-08-04
  • 3 回答
  • 0 關注
  • 515 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號