JUnit+Mockito无法Mock方法调用,报InvalidUseOfMatchersException错误求助
Hey there! Let's break down why you're hitting that InvalidUseOfMatchersException when mocking your isOwner method—this is a super common pitfall for folks new to Mockito, so we’ll get it sorted out.
Common Causes for This Exception
Let’s walk through the most likely reasons you’re seeing this error:
- Mixing matchers and raw values: Mockito doesn’t allow combining argument matchers (like
anyArgs(),anyString()) with real concrete values in the same method call. For example,when(xxx.isOwner("realUserId", anyLong())).thenReturn(true)will throw the error because the first parameter is a raw string while the second is a matcher. - Incorrect
doReturnsyntax: If you wrote something likedoReturn(someData).when(xxx.isOwner(anyArgs())), that’s wrong. Thewhen()call should only take your mock object, not the method invocation. The method call with matchers comes after thewhen()block. - Missing static imports for matchers: If you’re using
anyArgs()but haven’t imported Mockito’s static matcher methods, Mockito won’t recognize it as a valid matcher, leading to the exception. - Mocking a real instance: If the
xxxobject you’re trying to mock is a real, instantiated service (not created via@MockorMockito.mock()), calling mock methods on it will fail.
Fixes to Try
Let’s go step by step to fix this:
- Align your argument usage:
If you use any matchers, wrap all arguments in matchers. Useeq()to turn raw values into matchers when you need to target specific inputs. For example:// Correct: All arguments use matchers when(xxx.isOwner(eq("specificUserId"), anyLong())).thenReturn(true); - Fix your
doReturnsyntax:
The correct structure fordoReturnis:
Avoid putting the method call inside the// Right way: Pass mock to when(), then call the method with matchers doReturn(true).when(xxx).isOwner(anyArgs());when()parentheses—that triggers a real method call and breaks matcher logic. - Add static imports:
Make sure you’ve imported Mockito’s static methods to use matchers properly. Add this at the top of your test class:import static org.mockito.Mockito.*; - Verify your mock is a real Mock instance:
Ensure yourxxxobject is created as a mock, not a real service. Use either:- The
@Mockannotation (with@ExtendWith(MockitoExtension.class)for JUnit 5, or@RunWith(MockitoJUnitRunner.class)for JUnit 4), or - Manual mock creation:
YourService xxx = Mockito.mock(YourService.class);
- The
Example of Correct Mocking
Suppose your isOwner method has the signature boolean isOwner(String userId, Long resourceId)—here’s how to mock it properly with both styles:
Using when(...).thenReturn()
when(xxx.isOwner(anyString(), anyLong())).thenReturn(true); // Or for specific values when(xxx.isOwner(eq("testUser123"), eq(456L))).thenReturn(true);
Using doReturn(...).when()
doReturn(true).when(xxx).isOwner(anyString(), anyLong()); // Or for specific values doReturn(true).when(xxx).isOwner(eq("testUser123"), eq(456L));
Start by checking these points—chances are one of them is the root of your issue!
内容的提问来源于stack exchange,提问作者Bhagyesh
相关产品推荐
相关产品推荐

