当前位置: 首页 > 知识库问答 >
问题:

mockito.when giving invaliduseofMatchersException:此处检测到错误放置或错误使用的参数匹配器

燕鸿文
2023-03-14
@RunWith(MockitoJUnitRunner.class)
public class AuditServiceClientTest {

private MockMvc mockMvc;

@Mock
private RestTemplate restTemplate;

@Mock
AuditServiceClient auditServiceClient;

@Mock
ICommonDataService iCommonDataService;

private AuditServiceResponse auditServiceResponse;

private AuditServiceLog auditServiceLog;

private HttpEntity<AuditServiceLog> request;

@Before
public void setUp() throws Exception {

    MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
    headers.add("X-AGS-Client-Name", "test");
    headers.add("X-AGS-Group-Name", "test");
    headers.add("Content-Type", "application/json");
    auditServiceClient = new AuditServiceClientImpl();
    iCommonDataService = new CommonDataService();
    auditServiceLog = new AuditServiceLog();
    request = new HttpEntity<AuditServiceLog>(auditServiceLog, headers);
    auditServiceResponse = new AuditServiceResponse();
    auditServiceResponse.setStatus(String.valueOf(200));
    auditServiceResponse.setTimestamp("1990-01-01 00:00:01");
    auditServiceResponse.setDescription("Description");
    Mockito.when(restTemplate.postForObject(Mockito.anyString(), any(HttpEntity.class), ArgumentMatchers.eq(AuditServiceResponse.class)))
            .thenReturn(auditServiceResponse);
    String a = "test";
    ArrayList<Integer> mockedList = new ArrayList<Integer>();
    Mockito.when(iCommonDataService.getEnvValue(Mockito.anyInt(), Mockito.anyInt(), Mockito.anyString()))
            .thenReturn(a);
}

@Test
public void postTest() {

    AuditServiceResponse response = null;
    try {

        response = auditServiceClient.post("endpoint", auditServiceLog, true);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Assert.assertTrue(Integer.parseInt(response.getStatus() )== 200);
}
}

在setUp()方法下一行中:

 Mockito.when(iCommonDataService.getEnvValue(Mockito.anyInt(), Mockito.anyInt(), Mockito.anyString()))
        .thenReturn(a);

以下是错误:

org.mockito.exceptions.misusing.invaliduseofmatchersexception:在此处检测到错误放置或错误使用的参数匹配器:

->在com.auditService.test.auditServiceClientTest.setup(auditServiceClientTest.java:72)

when(mock.Get(anyInt())).ThenReturn(null);
doThrow(new RuntimeException()).when(mock).SomeVoidMethod(anyObject());
verify(mock).SomeMethod(contains(“foo”))

如果最后一个匹配器返回像any()一样的对象,但stubbed方法签名需要一个原语参数,在本例中,使用原语替代项,则该消息可能出现在NullPointerException之后。

当(mock.get(any()));//使用不当,将在(mock.get(anyInt()))时引发NPE
;//正确用法使用

这里有很多关于这个错误的文章,但没有一篇对我有用。mockito.anyint()是否存在任何问题,因为前面一行使用了mockito.anyString()并且效果良好。感谢任何帮助。

共有1个答案

南门向荣
2023-03-14

仔细查看您的测试代码:

iCommonDataService = new CommonDataService();
    ...
Mockito.when(iCommonDataService.getEnvValue(Mockito.anyInt(), Mockito.anyInt(), Mockito.anyString()))
            .thenReturn(a);

您正在模拟一个对象的方法,而该对象不是模拟的,这就是异常的原因。

由于您已经有一个通过@mock注释声明的该类的模拟,您可以简单地删除这一行ICommonDataService=new CommonDataService();。另一种方法是使用mockito.mock(CommonDataService.class)手动提供模拟。

 类似资料: