I have a factory method that creates objects to be used in unit tests. These objects all derive from the same base class:
public static <T extends BaseEntity> T modMake(Class<T> clazz)
{
try {
return clazz.newInstance();
} catch (InstantiationException e) {
// Should never happen
throw new AssertionError(e);
} catch (IllegalAccessException e) {
// Should never happen
throw new AssertionError(e);
}
}
Now I want to override a getter method from that base class, but just for the tests. I would usually do that with an anonymous class, for example (Node
being one of the subtaypes of BaseEntity
):
public static Node nodMake()
{
return new Node() {
@Override
public long ixGet() { return 1; }
};
}
Can I do that in the function using the Class
argument, too?