As a developer I should strive for low coupling between classes.
But low coupling doesn't mean "No coupling" so sometimes I have to allow some code flexibility and use "new" inside a domain model. (I'm sure everybody does this in real production code)
If I use "new" inside a method to create an object and I have to unit test this method I got a problem. Because I can't mock the objects I can't test the method in isolation. I know I can use dependency injection or I can send objects as parameters but I'm not talking about this clear cases.
Is this acceptable in practice?
Is there a better way? (I can't always inject)
Is this a unit test or an integration test?
PHP example:
class Car {
protected $objDoor;
public function createComponents() {
$this->objDoor = new Door();
}
}
new
. Just use a suitable mocking tool for your language of choice.new
-ed objects.