7

We try to verify the behaviour of an action with Mockito. The test code looks like this

final Type1 mock = mock(Type1.class);
new SomeAction<Type1>(mock).actionPerformed(null);

verify(mock).someMethod();

The method actionPerformed contains just the call of someMethod on the object provided in the constructor of Type1. Yet Mockito complains that the expected method call did not happen, instead a different method call happened. But the String representation of the two calls printed by Mockito are exactly the same!

Any explanation what is going on?

Update: ErrorMessage from Mockito

Argument(s) are different! Wanted:
type1.someMethod();
-> at xxx
Actual invocation has different arguments:
type1.someMethod();
-> at xxx
javamonkey79
  • 17,443
  • 36
  • 114
  • 172
Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • I tried this out, and it worked as you expect it should work (verify passes.) What version of Mockito are you using? I am on 1.8. Are you sure your null parameter isn't causing a different branch to be taken? – omerkudat Nov 24 '10 at 13:32
  • Version is 1.8.5; someMethod is a oneliner, so there really isn't a different part. – Jens Schauder Nov 24 '10 at 13:58
  • Could you provide a SSCCE please? – daveb Nov 24 '10 at 16:22
  • Just tried it out with version 1.8.5 and this all passes. Something you are doing must be different. – Lunivore Nov 24 '10 at 17:07
  • Are you sure you don't have arguments to those methods? Arguments with the same string representation? – Lunivore Nov 25 '10 at 21:44
  • someMethod has no arguments and is of type void ... but Type1 actually has a type parameter ... does that matter? – Jens Schauder Nov 28 '10 at 16:53

1 Answers1

4

This is a bit of a stretch, but check your toString implementations. I've ran into some irritating unit test scenarios where the expected and observed appeared to be the same from the unit test point of view when in reality they were different. In the end it was a variation in toString that caused me to believe there was a similarity when in reality there was not.

javamonkey79
  • 17,443
  • 36
  • 114
  • 172
  • since the method returns void and doesn't take arguments there isn't any toString involved ... I think. – Jens Schauder Dec 06 '10 at 18:28
  • The toString() representation showed in the unit test result tricked me once. To figure out if the 2 objects Mockito was complaining about are or not different, I overridden the toString again as it is in Object. – Crenguta S Aug 05 '16 at 11:22