Consider the following "production" code:
public class Foo<T extends Number> {
private Bar delegate;
private Class<T> numberClass;
public Foo(Class<T> numberClass) {
this.numberClass = numberClass;
}
public T doSomething() {
if (numberClass==Integer.class) {
return delegate.getIntValue();
} else if (numberClass==Float.class) {
return delegate.getFloatValue();
} else if (numberClass==Double.class) {
return delegate.getDoubleValue();
} // etc etc etc
return null;
}
}
This is based on a real situation I'm facing ... I have no control over the implementation of the delegate object.
So, how can I unit test this class with a JMockit @Tested
annotation? I can't use @Injectable
to inject a Class
value... it complains that Class
is not mockable. And I'm not seeking to mock it, just inject it. Unlike Strings or primitives or enums, where I could say @Injectable("foobar") String s;
and get a non-mocked injectable object, these semantics don't apply for Class
objects.
Obviously, my workaround is to hand-construct my Foo
in a @Before
method, and so I shall. But I can't help but feel like this situation should be possible with JMockit.