Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito Problems - InvalidUseOfMatchersException

I am doing a test using mockito, but I am getting this problems:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
2 matchers expected, 1 recorded:
-> at cl.gps.tms.planifications.planification.test.PlanificationCreateTest.setUp(PlanificationCreateTest.java:68)

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 PlanificationCreateTest use the SimpleQueryBus for create a generic query where de first parameter indicates what type of object is returned, and second parameters are filters on the query.

I want stub the SimpleQueryBus class (external library) returning a null (only for now)

The SimpleQueryBus code

public class SimpleQueryBus implements QueryBus {

    public <T, R> R handle(Class<R> clazz, T query) throws Exception {
    ...
    }
}

My test code

public class PlanificationCreateTest {

    private QueryBus queryBus;

    @Before
    public void setUp() throws Exception {
        queryBus = Mockito.mock(SimpleQueryBus.class);

        when(queryBus.handle(VehicleCollection.class, any(GetVehicle.class))).thenAnswer(null);

        ....
    }
}

UPDATE (SOLVED):

public class PlanificationCreateTest {

    private QueryBus queryBus;

    @Before
    public void setUp() throws Exception {
        queryBus = Mockito.mock(SimpleQueryBus.class);

        // first example
        when(queryBus.handle(any(Class.class), isA(VehicleAvailable.class))).thenReturn(null);          

        // second example
        vehicle = new VehicleCollection("001", "name", "tag", "brand", "model");            
        when(queryBus.handle(any(Class.class), isA(GetVehicle.class))).thenReturn(vehicle);

        ....
    }
}

Thanks...

like image 972
Hector Avatar asked Jul 24 '14 15:07

Hector


2 Answers

This occurs because you are using any() along with a real parameter VehicleCollection.class of type Class<VehicleCollection>.

Change it just like below, and you should be fine:

 when(queryBus.handle(any(VehicleCollection.class), any(GetVehicle.class))).thenAnswer(null);

Mockito explains why: http://mockito.googlecode.com/svn/tags/1.7/javadoc/org/mockito/Matchers.html

If you are using argument matchers, all arguments have to be provided by matchers.

E.g: (example shows verification but the same applies to stubbing):

    // Correct - eq() is also an argument matcher
    verify(mock).someMethod(anyInt(), anyString(), eq("third argument"));


    // Incorrect - exception will be thrown because third argument is given without argument matcher.
    verify(mock).someMethod(anyInt(), anyString(), "third argument");    
like image 173
squallsv Avatar answered Sep 21 '22 12:09

squallsv


This is also a problem for EasyMock

Basically whenever you use a Matcher on one argument, you need to use them on all the arguments

when(queryBus.handle(isA(VehicleCollection.class), any(GetVehicle.class))).thenAnswer(null);

Should work

I added isA(Class) which creates a matcher to make sure the argument type matches the given class.

like image 30
dkatzel Avatar answered Sep 22 '22 12:09

dkatzel