How to Unit Test Classes Which Create New Objects

Learn how to conduct effective unit tests.

First of all, I will start with a disclaimer that I am a strong proponent of using the simple factory programming idiom and by extension of using the Factory Method Pattern, instead of creating objects inside classes. The factory idiom helps you to insulate your code to changes thereby adhering to the Open to Extension Close to modification principle of object-oriented programming.

You may also like: Unit Testing: The Good, Bad, and Ugly

Also, the idea is to write testable code, not to write a hack to test code.