Closed GoogleCodeExporter closed 8 years ago
Hi,
I'm not sure what your problem is, or if there's a real issue with Mockito but
here's the Java compiler sees things.
when(toTestMock.mymethod(any(GenericHelper.class))).thenReturn(" Mocked method
called!");
-> will be compiled to : INVOKEVIRTUAL MockitoTest$ToTest.mymethod
(LMockitoTest$GenericHelper;)Ljava/lang/String;
when(toTestMock.mymethod(any())).thenReturn(" Mocked method called!");
-> will be compiled to : INVOKEVIRTUAL MockitoTest$ToTest.mymethod
([Ljava/lang/Object;)Ljava/lang/String;
when(toTestMock.mymethod(eq(castedWithGeneric))).thenReturn(" Mocked method
called!");
-> will be compiled to : INVOKEVIRTUAL MockitoTest$ToTest.mymethod
([LMockitoTest$GenericHelper;)Ljava/lang/String;
However the line
toTestMock.mymethod(listMock)
-> will be compiled to : INVOKEVIRTUAL MockitoTest$ToTest.mymethod
([LMockitoTest$GenericHelper;)Ljava/lang/String;
Which is the same as the third stub. If your actual invocation gets compiled to
one of the other form it'll work as expected. Unfortunately we don't really
want to greedly stub overloaded declaration.
Also note that at the moment the javadoc of 'any' says it doesn't do any type
checks, it's a short version for 'anything'. Too bad many people mistake that
with isA. We will certainly change that later.
Hope that helps a bit.
Original comment by brice.du...@gmail.com
on 29 Jan 2013 at 9:35
I have consulted with a more experienced colleague here, and it turned out that
the issue is caused by how generic works in Java. It seems that this problem is
not related to Mockito.
this definition:
GenericHelper<Collection> castedWithGeneric = listMock;
does not match the intended method's parameter:
String mymethod(GenericHelper<Collection<?>> gh)
because GenericHelper<Collection<?>> does not extend GenericHelper<Collection>
this relation is only true between the Collection - Collection<?>
so, the compiler picks up the next relevant method, the
String mymethod(GenericHelper<?>... ts)
Mockito assigns the stub to this method, which does not match the actual call's
parameter.
I should have defined the mocked object as:
GenericHelper<Collection<?>> castedWithGeneric = mock(GenericHelper.class);
Original comment by Attila.P...@gmail.com
on 30 Jan 2013 at 11:01
OK, glad you overcame your problem.
Don't hesitate to go to the mailing list if you are unsure if this is a bug
with mockito ;)
Cheers
Brice
Original comment by brice.du...@gmail.com
on 30 Jan 2013 at 11:17
Original issue reported on code.google.com by
Attila.P...@gmail.com
on 28 Jan 2013 at 4:14