Search code examples
javajunitmockitopowermockito

InvalidUseOfMatchersException when using Matchers.eq() with other mocked values


In my JUnit class i have the below code:

@Mock
private HttpServletRequest servletRequest;

@Mock
WidgetHelper widgetHelper;

@Mock
JSONObject jsonObject;

@Mock
Date date;

verify(widgetHelper, times(1)).invokeAuditService(servletRequest, date, anyString(), 
                        Matchers.eq("Member_Servicing_Email_Update"), jsonObject, anyString());

I'm getting the below output:

This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));

The thing I want to achieve is: I want to test if the 4th argument to the method contains the string "Member_Servicing_Email_Update" or not. Rest of the arguments can be mocked. I used Matchers.anyObject() for others and I got error saying cannot match anyObject to java.lang.String, Date, HttpServlet and so on. What needs to be done here? I also just put eq("Member_Servicing_Email_Update") but eq was not recognized.


Solution

  • Add Matchers.eq for all raw parameters:

    verify(widgetHelper, times(1)).invokeAuditService(Matchers.eq(servletRequest), Matchers.eq(date), anyString(), 
                    Matchers.eq("Member_Servicing_Email_Update"), Matchers.eq(jsonObject), anyString());
    

    When using matchers, all arguments have to be provided by matchers