Status: New
Owner: ----
Labels: Type-Defect Priority-Medium
New issue 415 by
Attila.P...@gmail.com: overloaded methods with vararg
variants cause problem
http://code.google.com/p/mockito/issues/detail?id=415
<code>
package org.test.mockito;
import java.util.Collection;
import org.junit.Test;
import org.mockito.Mockito;
public class MockitoTest extends Mockito {
ToTest toTestMock = mock(ToTest.class);
GenericHelper<Collection> listMock = mock(GenericHelper.class);
class GenericHelper<T> {
T t;
T getT() {
return t;
}
}
class ToTest {
String mymethod(Object... os) {
return "Object array, size: " + os.length;
}
String mymethod(GenericHelper<?>... ts) {
return "GenericHelper array, size: " + ts.length;
}
String mymethod(GenericHelper<Collection<?>> gh) {
return "GenericHelper, value: " + gh.getT();
}
String mymethod(Collection<?> collection) {
return "collection, size: " + collection.size();
}
}
@Test
public void test() {
GenericHelper<Collection> castedWithGeneric = listMock;
//when(toTestMock.mymethod(any(GenericHelper.class))).thenReturn("
Mocked method called!"); // returns null
//when(toTestMock.mymethod(any())).thenReturn(" Mocked method
called!"); // returns null
when(toTestMock.mymethod(eq(castedWithGeneric))).thenReturn("
Mocked method called!"); //works
//when(toTestMock.mymethod(castedWithGeneric)).thenReturn(" Mocked
method called!"); //works
String s = toTestMock.mymethod(listMock);
System.out.println(s);
}
}
</code>
Above test code fails when the lines marked with null is commented in to
work.
The general problem is, that by mocking a class, that has several
overloaded methods, including some with varargs parameter, and in the above
generic structure, the type based argument matchers seems don't work.
Expected output would be the "Mocked method called" in all lines, but with
those remarked with null, null will return - method is not stubbed as it
should have.
What version of the product are you using? 1.8.4
On what operating system? Win7
I found the problem during JPA Criteria API mocking, the actual mocked
object was the ExpressionImpl object, the method is the in() method, that
has 4 overloaded variants, from which 2 uses vararg. Inside the Mockito
code InvocationMatcher.hasSameMethod showed, that while i was calling with
the Expression parameter method, the candidate's type was Expression[], and
this unmatch led to returning null. I will stay contact, and give
additional information as needed.