I have the following interface that I would like to mock when testing a class.
public interface Evaluator<T extends Action>
{
public abstract double evaluate(List<T> actionSequence);
}
Is it possible to create a mock that has a generic type of Action? ie
I want the mock objet to have the generic type ChildAction. I would
like to have something like this, but this will throw a
"java.lang.ClassCastException: java.lang.Class cannot be cast to
java.lang.reflect.ParameterizedType".
Evaluator<ChildAction> evaluator = Mockito.mock(Evaluator.class);
Also the class under test will use reflection of the object
implementing the Evaluator interface to find the generic type so it
can create a Class<T> of it as it needs to be fed into an API. I used
reflection so I didnt have to have a "Class<T> getClass()" method in
the interface.
But I guess that will not play nice with the mockito logic as the
mocked object is not a real implementation of the Evaluator interface?
public class ActionBasedSequenceRule
{
public static Class<? extends Action> getActionClass(Evaluator<?
extends Action> evaluator)
{
....
Type t =
evaluator.getClass().getGenericInterfaces()[0].getActualTypeArguments()[0];
....
}
}
So what do you think, is this even remotly possible or just a dumb idea?
Regards
//Erik