gpt4 book ai didi

java - 如何多次模拟具有不同参数的一种方法?

转载 作者:行者123 更新时间:2023-12-02 09:31:03 30 4
gpt4 key购买 nike

我有一些测试方法:

@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.

我如何模拟这个方法?

最佳答案

错误消息告诉您:

stubbing the same method multiple times using 'given().will()' or 'when().then()' API Please use 'will().given()' or 'doReturn().when()' API for stubbing.

关于java - 如何多次模拟具有不同参数的一种方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57974644/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com