Solution 1 :
I solved after having wasted a couple of hours, and I thought that this behaviour of Mockito is a bit tricky.
The problem was due to a mismatch of Maps values’ types that in the test I passed as “map_param_2” a “10” as String, while in the actual call to that method the param was passed as a simple int, but looking at the message it’s not possible to understand the difference because in both there is written map_param_2=10
.
After climbing some mirrors I wrote down a custom matcher using Mockito’s argThat
and cycling the maps’ parameters I noticed that they were of different types.
Solution 2 :
Problem is that you have two instances of Map. In this case you had to create variable Map and then use it while method execution and while verification. Then you would compare one instance.
Solution:
String param = "string_param";
Map<> map = ObjectUtil.map("map_param_1", "value", "map_param_2", "10");
myClass.myMethod(param, map);
verify(myClass, times(1)).myMethod(param , map);
Try to use any()
functionality if you want to check if method was executed with no matter of parameters.
import static org.mockito.ArgumentMatchers.any;
verify(myClass).myMethod(any(String.class), any(LinkedHashMap.class));
Was executed exactly one time:
verify(myClass, times(1)).myMethod(any(String.class), any(LinkedHashMap.class));
Problem :
Writing a test like this using Mockito’s verify
:
verify(myClass, times(1)).myMethod("string_param", ObjectUtil.map("map_param_1", "value", "map_param_2", "10"))
(ObjectUtil.map
is a util method which returns a LinkedHashMap<String, Object>
)
I obtain this error:
Argument(s) are different! Wanted: myClass.myMethod(
“string_param”,
(LinkedHashMap) {map_param_1=value, map_param_2=10} );
-> at …Actual invocation has different arguments:
myClass.myMethod(
“string_param”,
(HashMap) {map_param_1=value, map_param_2=10} );
Looking at the error I would say that the equals fails because on one side there is a LinkedHashMap
and at the other one an HashMap
, but changing the type doesn’t solve the problem, and the parameters look exactly the same, what it could be?
Comments
Comment posted by Alexander Stohr
good find! seen similar problems just recently with type mismatches – it was the boolean type versus the Boolean class. And there were others of supposed similar category, like a string being reported with and without double quotes with the result of regarding this non-equal. – in my works the final clue is not yet out (thats why i have landed here). but its already a great deal knowing that the reported hints on comparison mismatches are incomplete in regards that only values are shown but the as well compared types are left out. (it makes not much sense to do so but its that way.)
Comment posted by Alexander Stohr
PS: for the boolean vs. Boolean i had to use the type cast prefix (boolean) on my true/false value.
Comment posted by Mauro Piccotti
Thanks Misha, this was my first “approximation” but I needed to check also the parameters. Anyway I already added the “solution”, I wrote the question on StackOverflow only because I wasted time on it and I thought it could be useful for somebody else.