lundi 29 janvier 2018

Are depencency injection and mocking absolutely essential for good unit tests? [on hold]

I have read and heard many times that good unit tests test one and only one thing. For this purpose, I usually use dependency injection to separate components from each other as much as possible. However, this usually implies a lot of interfaces with a single implementing classes (Foo interface with DefaultFoo implementation), which usually makes the code look weird.

I think it is considered a general truth that we need dependency injection and we need to mock dependencies in unit tests, otherwise we would not only test one component, but also components that it uses, i.e. its dependencies. However, if I write unit tests for dependencies too, I can always find out the cause in case of a failing unit test quite fast: if the unit test of a component fails, then first check the unit tests of its dependencies, too. I have also read somewhere (don't remember where) that a unit test should essentially be a black box test, which is violated by using mock objects.

I would like to know if it is generally accepted to write unit tests without dependency injection and mocking, so that one unit test also tests the dependencies of the component under test. My goal is to avoid overcomplicated hieararchies with unnecessary interfaces. I will be also thankful for references to books, but I am starting a new project now and I would like to decide what principles I use for testing, so I would be more happy with a direct answer. Thank you.

Aucun commentaire:

Enregistrer un commentaire