Issue with Mockito.Any for overloaded methods

Viewed 313

I have two methods in my Java class as below Method 1:

public ResponseEntity<T> callMethod(String param1,Map<String, String> param2, Object param3,HttpMethod param4,Class<T> param5)
        

Method 2

public ResponseEntity<T> callMethod(Map<String, String> param1, Object param2,
            HttpMethod param3, Map<String, ?> param4,
            final Class<T> param5) {

I have written a unit test case where when i try to call the method as below

callMethod(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(),
                eq(ServiceResponse.class))).thenReturn(responseEntity)

it fails with the below error

reference to callMethod is ambiguous

both method callMethod (java.util.Map<java.lang.String,java.lang.String>,B,org.springframework.http.HttpMethod,java.util.Map<java.lang.String,?>,java.lang.Class) and method callMethod(java.lang.String,java.util.Map<java.lang.String,java.lang.String>,B,org.springframework.http.HttpMethod,java.lang.Class) match

How can this be resolved. There are many unit test cases and i want to fix the class than the unit test cases. Is there a way?

1 Answers

Yes, you can put types in the any() like any(MyClass.class) there are also some built in ones. If you want to call your first method you can use the anyString() and anyMap() ones.

callMethod(Mockito.anyString(), Mockito.anyMap(), Mockito.any(), Mockito.any(HttpMethod.class), eq(ServiceResponse.class))).thenReturn(responseEntity)
Related