1

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.

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
Coder17
  • 767
  • 4
  • 11
  • 30
  • You need to use either matchers or concrete values for all parameters. You can't mix them like you do. – daniu Jan 08 '20 at 08:35

1 Answers1

0

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

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • PowerMockito.doNothing().when(widgetHelper).invokeAuditService(Matchers.eq(servletRequest), Matchers.eq(date), anyString(), Matchers.eq("Member_Servicing_Email_Update"), Matchers.eq(jsonObject), anyString()); verify(widgetHelper, times(1)).invokeAuditService(Matchers.eq(servletRequest), Matchers.eq(date), anyString(), Matchers.eq("Member_Servicing_Email_Update1"), Matchers.eq(jsonObject), anyString()); This code is giving me unfinished stubbing exception. here invokeAuditService method is void – Coder17 Jan 08 '20 at 09:53
  • @Coder17 can you ask a new question? – Ori Marko Jan 08 '20 at 10:12
  • https://stackoverflow.com/questions/59643836/unfinishedstubbingexception-when-working-with-donothing-method-for-void-methods – Coder17 Jan 08 '20 at 10:26