Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unit test instance creation?

I have a Carpenter class that does it's work using a Lathe and a Wood object.

class Carpenter
{
    function Work()
    {
        $tool = new Lathe();
        $material = new Wood();
        $tool->Apply($material);
    }
}

Lathe depends on an interface called Material, so I can easily unit test Lathe by giving it a fake Material in my unit test. Wood doesn't depend on anything, so it can also be easily tested.

interface Material {
    // Various methods...
}

interface Tool {
    function Apply(Material $m);
}

class Wood implements Material  {
    // Implementations of Material methods
}

class Lathe {
    function Apply(Material $m) {
        // Do processing
    }
}

However, Carpenter depends on the concrete classes Lathe and Wood because it has to create instances of them. That means that as it currently stands, I cannot unit test the Work() method without inadvertantly bringing Lathe and Wood under test.

How should I change my design to unit test Carpenter?

like image 409
ctford Avatar asked Dec 14 '09 11:12

ctford


1 Answers

There's a couple of different directions you can take here:

  • Use Constructor Injection and simply inject the tool and the material instances into the carpenter.
  • If injecting instances doesn't work for some reason (perhaps because you need to create new instances for every invocation of the Work method), you can inject Abstract Factories instead.
  • You can also use the Factory Method approach described by ctford, but that requires you to also create test-specific overrides to be able to unit test, and while that's a completely valid thing to do, it's just more work and in many cases the other alternatives are better and more flexible.
like image 107
Mark Seemann Avatar answered Nov 15 '22 15:11

Mark Seemann