1

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 ;-)

pvpkiran
  • 25,582
  • 8
  • 87
  • 134
W vd L
  • 625
  • 9
  • 26
  • Personally I would not mock those methods, because that is basic Java.lang. I think it's totally fine, to test if the factory method returns an instance of an object that is equal to new T() for instance. – Christian Ziegler May 16 '17 at 13:00
  • How do i get then in the catch? as for now it's always working. – W vd L May 16 '17 at 13:01
  • Yes sorry, I just noticed I missed that. Let me see if I can get something working. – Christian Ziegler May 16 '17 at 13:02
  • Check this: http://stackoverflow.com/questions/14292863/how-to-mock-a-final-class-with-mockito – Christian Ziegler May 16 '17 at 13:14
  • That link aint helping me :( as far i checked it i still can't spy on/mock the fooClass.newInstance() – W vd L May 16 '17 at 14:07
  • 2
    Rather than mocking, would it not be easier set up a test with real classes that will cause the required exceptions to be thrown? Example: To get an InstantiationException you can pass in an abstract class (defined as a nested class inside your test) and to get an IllegalAccessException you can send in a class with a private no-args constructor. Then test if RuntimeException was thrown. – mdewit May 16 '17 at 14:23
  • @mdewit if you post your comment as reply, i can flag it as the right answer, as it is imo closer to the real-life-scenario and more elegant as mocking could be – W vd L May 17 '17 at 08:00
  • Ok I will post it as an answer, glad it helped :) – mdewit May 17 '17 at 08:10

1 Answers1

0

Rather than mocking, would it not be easier set up a test with real classes that will cause the required exceptions to be thrown?

Example: To get an InstantiationException you can pass in an abstract class (defined as a nested class inside your test) and to get an IllegalAccessException you can send in a class with a private no-args constructor. Then test if RuntimeException was thrown.

mdewit
  • 2,016
  • 13
  • 31
  • I have a similar situation where i am getting java.lang.NullPointerException at javax.xml.validation.SchemaFactory.newSchema in the schemaFactory method */ public Schema newSchema(URL schema) throws SAXException { return newSchema(new StreamSource(schema.toExternalForm())); } while writing junit...how to handle this – not-a-bug Aug 17 '20 at 10:48