dimanche 1 mars 2015

ClassCastException using Mockito for generic method



I am using mockito to test a generic method. But i get a ClassCastException when i run the junit-test.


The method under test looks like this:



public ExampleClass {
public <E> E randomObject(List<E> list) {
E e = list.get(0);
return e;
}
}


The mock looks like this:



ExampleClass exampleMock = mock(ExampleClass.class);
List listMock = mock(List.class);
when(exampleMock.randomObject(Matchers<List<String>any())).thenReturn("Hello");
when(exampleMock.randomObject(Matchers.List<Integer>any())).thenReturn(20);


The Exception appears at the second definition of when-method. It looks like the method accpets only one type which isn't changeable. But why is that so? When i use the generic method with two different types in plain java the Exception won't appear.


Can someone please help?




Aucun commentaire:

Enregistrer un commentaire