<p>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?</p>

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

<p>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.</p>

[{'answer_id': 37316, 'author': 'Cem Catikkas', 'author_id': 3087, 'author_profile': 'https://Stackoverflow.com/users/3087', 'pm_score': 6, 'selected': True, 'text': "<p>Since I don't know how your factory method looks like, all I can advise right now is to </p>\n\n<ol>\n<li><p>Check to see the object is the correct concrete implementation you were looking for:</p>\n\n<pre><code>IMyInterface fromFactory = factory.create(...);  \nAssert.assertTrue(fromFactory instanceof MyInterfaceImpl1);\n</code></pre></li>\n<li><p>You can check if the factory setup the concrete instances with valid instance variables.</p></li>\n</ol>\n"}, {'answer_id': 37318, 'author': 'izb', 'author_id': 974, 'author_profile': 'https://Stackoverflow.com/users/974', 'pm_score': 0, 'selected': False, 'text': "<pre><code>if (myNewObject instanceof CorrectClass)\n{\n    /* pass test */\n}\n</code></pre>\n\n<p><strong>update:</strong></p>\n\n<p>Don't know why this got marked down, so I'll expand it a bit...</p>\n\n<pre><code>public void doTest()\n{\n    MyInterface inst = MyFactory.createAppropriateObject();\n    if (! inst instanceof ExpectedConcreteClass)\n    {\n        /* FAIL */\n    }\n}\n</code></pre>\n"}, {'answer_id': 39369, 'author': 'Marcio Aguiar', 'author_id': 4213, 'author_profile': 'https://Stackoverflow.com/users/4213', 'pm_score': 0, 'selected': False, 'text': "<p>@cem-catikkas I think it would be more correct to compare the getClass().getName() values. In the case that MyInterfaceImpl1 class is subclassed your test could be broken, as the subclass is instanceof MyInterfaceImpl1. I would rewrite as follow:</p>\n\n<pre><code>IMyInterface fromFactory = factory.create(...);  \nAssert.assertEquals(fromFactory.getClass().getName(), MyInterfaceImpl1.class.getName());\n</code></pre>\n\n<p>If you think this could fail in some way (I can't imagine), make the two verifications.</p>\n"}, {'answer_id': 34199097, 'author': 'Undreren', 'author_id': 1264675, 'author_profile': 'https://Stackoverflow.com/users/1264675', 'pm_score': 6, 'selected': False, 'text': "<h1>What you are trying to do is not Unit Testing</h1>\n<p>If you test whether or not the returned objects are instances of specific concrete classes, you aren't unit testing. You are integration testing. While integration testing is important, it is not the same thing.</p>\n<p>In unit testing, you only need to test the object itself. If you assert on the concrete type of the abstract objects returned, you are testing over the implementation of the returned object.</p>\n<h1>Unit Testing on Objects in general</h1>\n<p>When unit testing, there are four things, you want to assert:</p>\n<ol>\n<li>Return values of queries (non-void methods) are what you expect them to be.</li>\n<li>Side-effects of commands (void methods) modify the object itself as you expect them to.</li>\n<li>Commands send to other objects are received (This is usually done using mocks).</li>\n</ol>\n<p>Furthermore, you only want to test what could be observed from an object instance, i.e. the public interface. Otherwise, you tie yourself to a specific set of implementation details. This would require you to change your tests when those details change.</p>\n<h1>Unit Testing Factories</h1>\n<p>Unit testing on Factories is really uninteresting, because <strong>you are not interested in the behavior of the returned objects of queries</strong>. That behavior is (hopefully) tested elsewhere, presumable while unit testing that object itself. You are only really interested in whether or not the returned object has the correct <em>type</em>, which is guaranteed if your program compiles.</p>\n<p>As Factories do not change over time (because then they would be &quot;Builders&quot;, which is another pattern), there are no commands to test.</p>\n<p>Factories are responsible for instantiating objects, so they should not depend on other factories to do this for them. They <em>might</em> depend on a Builder, but even so, we are not supposed to test the Builder's correctness, only whether or not the Builder receives the message.</p>\n<p>This means that all you have to test on Factories is whether or not they send the messages to the objects on which they depend. If you use Dependency Injection, this is almost trivial. Just mock the dependencies in your unit tests, and verify that they receive the messages.</p>\n<h1>Summary of Unit Testing Factories</h1>\n<ol>\n<li>Do not test the behavior nor the implementation details of the returned objects! Your Factory is not responsible for the implementation of the object instances!</li>\n<li>Test whether or not the commands sent to dependencies are received.</li>\n</ol>\n<p>That's it. If there are no dependencies, there is nothing to test. Except maybe to assert that the returned object isn't a <code>null</code> reference.</p>\n<h1>Integration Testing Factories</h1>\n<p>If you have a requirement that the returned abstract object type is an instance of a specific concrete type, then this falls under integration testing.</p>\n<p>Others here have already answered how to do this using the <code>instanceof</code> operator.</p>\n"}, {'answer_id': 51817768, 'author': 'Federico Gatti', 'author_id': 8615621, 'author_profile': 'https://Stackoverflow.com/users/8615621', 'pm_score': 0, 'selected': False, 'text': '<p>If your Factory returns a concrete instance you can use the <strong>@Parameters</strong> annotation in order to obtain a more flexible automatic unit test.</p>\n\n<pre><code>package it.sorintlab.pxrm.proposition.model.factory.task;\n\nimport org.junit.Test;\n\nimport java.util.Arrays;\nimport java.util.Collection;\n\nimport org.junit.runner.RunWith;\nimport org.junit.runners.Parameterized;\nimport org.junit.runners.Parameterized.Parameters;\n\nimport static org.junit.Assert.*;\n\n@RunWith(Parameterized.class)\npublic class TaskFactoryTest {\n\n    @Parameters\n    public static Collection&lt;Object[]&gt; data() {\n        return Arrays.asList(new Object[][] {\n                { "sas:wp|repe" , WorkPackageAvailabilityFactory.class},\n                { "sas:wp|people", WorkPackagePeopleFactory.class},\n                { "edu:wp|course", WorkPackageCourseFactory.class},\n                { "edu:wp|module", WorkPackageModuleFactory.class},\n                { "else", AttachmentTaskDetailFactory.class}\n        });\n    }\n\n    private String fInput;\n    private Class&lt;? extends TaskFactory&gt; fExpected;\n\n    public TaskFactoryTest(String input, Class&lt;? extends TaskFactory&gt; expected) {\n        this.fInput = input;\n        this.fExpected = expected;\n    }\n\n    @Test\n    public void getFactory() {\n        assertEquals(fExpected, TaskFactory.getFactory(fInput).getClass());\n    }\n}\n</code></pre>\n\n<p>This example is made using <em>Junit4</em>. You can notice that with only one line of code you can test all the result of your Factory method.</p>\n'}]
['https://Stackoverflow.com/questions/37310', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3576/']