0
 @Test
@DisplayName("Success scenario for Card Replace for Lost")
void testcardReplaceLostSuccess() throws Exception {
    final CardReplaceRqst cardReplaceRqst = mockCardReplaceRequest("Lost");
    when(cardBlockService.sendBlockRequest(cardReplaceRqst)).thenReturn(new ResponseEntity<>(any(), HttpStatus.OK));
    when(cardReplaceService.replaceCard(ArgumentMatchers.eq(cardReplaceRqst), any(CardType.class)))
        .thenReturn(mockCardReplaceResponse());

    mockMvc
        .perform(MockMvcRequestBuilders.post(REPLACE_REQUEST_URL).contentType(MediaType.APPLICATION_JSON_VALUE)
            .content(TestUtils.toJson(cardReplaceRqst)).characterEncoding("utf-8"))
        .andExpect(MockMvcResultMatchers.status().isOk()).andExpect(jsonPath("$.successCode").value("Success"));
}

When I run this code, I get the below error.

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 

Invalid use of argument matchers! 2 matchers expected, 3 recorded:

Any suggestions?

p.s. I had to add ArgumentMatcher.eq() because of a sonar rule "Add an "eq()" argument matcher on this parameter."

  • Does this answer your question? [Invalid use of argument matchers](https://stackoverflow.com/questions/24468456/invalid-use-of-argument-matchers) – user2340612 Jul 29 '22 at 17:32

1 Answers1

0

Using any() in the response thenReturn(new ResponseEntity<>(any(), HttpStatus.OK)); is illegal and causes a mess with subsequent stubbing

See Argument Matchers

Matcher methods like any(), eq() do not return matchers. Internally, they record a matcher on a stack and return a dummy value (usually null). This implementation is due to static type safety imposed by the java compiler. The consequence is that you cannot use any(), eq() methods outside of verified/stubbed method.

Lesiak
  • 22,088
  • 2
  • 41
  • 65