For unit testing I want to test good and bad scenario's
I'm getting a bit stuck on the Factory.createFoo() method. How do I write proper unit tests for this (with Mockito)
public class Bar extends Foo {
public Bar() {}
public Bar(Scenario scenario){
...DoStuff..
}
}
public static <T extends Foo> T createFoo(Class<T> fooClass) throws RuntimeException {
try {
return fooClass.newInstance();
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
throw new RuntimeException("Could not create the Foo: " + fooClass.getSimpleName(), e);
}
}
public static <T extends Foo> T createFoo(Scenario scenario, Class<T> fooClass) throws RuntimeException {
try {
return fooClass.getDeclaredConstructor(Scenario.class).newInstance(scenario);
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
throw new RuntimeException("Could not create the needed Foo: " + fooClass.getSimpleName(), e);
}
}
I would like to be able to mock the following parts:
fooClass.newInstance();
fooClass.getDeclaredConstructor(Scenario.class).newInstance(scenario);
I've found some factory examples, but all without the generic
Class<T>
making those examples invalid for me.
How to proceed to test these kind of factory methods? If there is a design flaw that is making this non testable, don't hesitate to point me those flaws ;-)