Checking the results of a Factory in a unit test

Tags: , ,



I have developed some classes with similar behavior, they all implement the same interface. I implemented a factory that creates the appropriate object and returns the interface. I am writing a unit test for the factory. All you get back is an interface to the object. What is the best way to test that the factory has worked correctly?

I would like to know the answer in Java, but if there is a solution that crosses languages I would like to know it.

Number 2. in the answer, would be done like the other answer? If so I will mark the other answer accepted as well and reword my question to adress both a factory where an interface is returned and you have no clue what type of concrete class implemented the interface, and the case where you do know what concrete class was used.

Answer

Since I don’t know how your factory method looks like, all I can advise right now is to

  1. Check to see the object is the correct concrete implementation you were looking for:

    IMyInterface fromFactory = factory.create(...);  
    Assert.assertTrue(fromFactory instanceof MyInterfaceImpl1);
    
  2. You can check if the factory setup the concrete instances with valid instance variables.



Source: stackoverflow