Org.mockito.exceptions.misusing.InvalidUseOfMatchersException:

My method in the service and testing class:

public void updateSubModuleOrder(Long[] data, Long moduleSysId, Long userId) { try { for (int i = 0; i < data.length; i++) { SubModule subModule=new SubModule(); int temp = i + 1; userSubmodule.setDsplySeq(temp); userSubModuleDao.saveOrUpdate(userSubmodule); @Test public void testupdateSubModuleOrder(){ UserModuleServiceImpl userModuleServiceImpl = new UserModuleServiceImpl(); UserSubModuleDao userSubModuleDao = mock(User//set the required param ,some code here// UserSubModuleId userSubModuleId=new UserSubModuleId(); //some code// when(userSubModuleDao.findById((any(UserSubModuleId.class)),false)).thenReturn(userSubModule); when(userSubModuleDao.saveOrUpdate(any(UserSubModule.class))).thenReturn(null); userModuleServiceImpl.updateSubModuleOrder(data, moduleSysId, userId); };* 

I get an error

 FAILED: testupdateSubModuleOrder org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers! 2 matchers expected, 1 recorded: -> at com.TestUserModuleServiceImpl.testupdateSubModuleOrder(TestUserModuleServiceImpl.java:267) 

This exception may occur if matches are combined with raw values:

 //incorrect: someMethod(anyObject(), "raw String"); 

When using matches, all arguments must be provided by the match. For example:

 //correct: someMethod(anyObject(), eq("String by matcher")); 

The findbyID method is a baseDao method that my dao extends. This is not final or static, but still I get this problem.

+7
java junit mockito matcher
source share
1 answer

You either do not need to specify matches, or all arguments need matches. So:

 when(userSubModuleDao.findById((any(UserSubModuleId.class)),false)) 

it should be:

 when(userSubModuleDao.findById(any(UserSubModuleId.class), eq(false))) 

(I removed the excess brackets around the call to any .)

From Matchers documentation :

Attention:

If you use argument arguments, all arguments must be provided using mappings.

+22
source share

All Articles