I got:-
Invalid use of argument matchers!
0 matcher expected, 1 recorded:
atInputStreamResource isr=new InputStreamResource(new ByteArrayInputStream(anyString().getBytes()));

- 1
- 1
-
1We can only mock calls on mock-objects. Thus, we should define `InputStreamResource isr = mock(InputStreamReader.class);`. I am not quite sure what you are trying to achieve with the "*mocked*" constructor call though. – Turing85 Aug 27 '21 at 21:43
-
Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Aug 30 '21 at 06:18
1 Answers
anyString()
is an argument matcher. It cannot be used in an "algebraic" sense (representing any value in your general program logic) or an "arbitrary value supplier" sense (in which it would supply an arbitrary string or other value to ensure your test works). Instead, it is meant to match your expectations against real interactions with your mock, either beforehand to determine the appropriate return value or behavior (when
) or afterwards to assert that the call happened (verify
).
Rules for matchers:
- Matchers come from static method calls on the
ArgumentMatchers
andAdditionalMatchers
object.MockitoHamcrest.argThat
andArgumentCaptor.capture
also count. - You can only use it in calls to
when
, as inwhen(mock.method(argumentOrMatcher))).thenVerb
ordoVerb(...).when(mock).method(argumentOrMatcher)
, orverify
as inverify(mock).method(argumentOrMatcher)
. - The matcher has to replace the entire argument; you cannot call methods on the value returned by a matcher, and it cannot appear within any other call (aside from other matchers like
AdditionalMatchers.and
). - Due to the way Mockito works, you'll need to be consistent: in a given call to
when
orverify
, all of the arguments have to be matchers, or none of them can be matchers (in which case each is matched as if withObject.equals
orArgumentMatchers.eq
).
Because you're using a matcher outside of those rules, there is 1 matcher recorded where there are 0 matchers expected. You'll need to write a test using your own arbitrary value where you currently have anyString
. If your test still requires Mockito, you can then pass that same string into when
or verify
, or use a matcher like anyString
there if needed.

- 90,959
- 16
- 217
- 251