Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.8k views
in Technique[技术] by (71.8m points)

Issue with Mockito.Any for overloaded methods

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...