Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – Mockito, verify and HashMap: Argument(s) are different

Posted on November 11, 2022 By

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?

READ  [FIXED] android - NonNull problem in MainActivity.java flutter
Powered by Inline Related Posts

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.

Android Tags:android, java, mockito

Post navigation

Previous Post: [FIXED] android – Convert BMP image to PNG or JPG in Java without loading the entire image in memory
Next Post: [FIXED] xml – Android – prevent TextView to push out another element inside LinearLayout

Related Posts

[FIXED] android – Firebase Cloud Messaging Questions About Overrided onNewToken Function Android
[FIXED] delphi – on android, does the Documents Path will always be /data/data/mypackagename/files/? Android
[FIXED] android – viewModel not returning updated response Android
[FIXED] java – ZERO_RESULTS in Retrieving the place type of a veterinary_care and pet_store Android Studio Android
[FIXED] java – having problem with installing genymotion in android studio Android
[FIXED] Saving Bitmap to External Storage in Android API 28 Result in 0 B file Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What is a good substitute for condensed milk?
  • ¿Como el cuerpo te avisa cuando te va a dar un infarto?
  • What is the luxury brand of Jaguar?
  • Who is Big poppa baseball player?
  • What material are foam runners?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme