In a server application, I have a class called Controller
which receive the incoming request. I also have a class called Service
which do business logic and a class called Database
. All classes are "public", but only one of them is really reachable from the "outside", the Controller
.
I am in a dilemma, should I only test the Controller
since it is the only callable class or should I test all 3 classes separately?
Pros for testing only the Controller
:
- Dead code easy to find, since it will not be covered
- Refactor won't break the tests, the
Service
class is only an implementation detail - Some languages have the "internal" keyword that suggest that the class should be consider as an implementation detail, thus to be treated as private
- Tests won't overlap on each other
Cons:
- Harder to know how a method in
Service
andDatabase
are tested - It is the same an integration test
- Setup is harder since the
Controller
depend everything. By harder I mean that I will have to create aDatabase
which will be injected into theService
which will be injected into theController
vs only mocking theService
which will be injected to theController
.
Service
andDatabase
classes as internal, while some developers would simply call them a class.Service
nor aDatabase
class is an implementation detail of aController
class, merely that they are dependencies which I would assume are themselves tested in isolation. I think you should either revise your example or your usage of internal class in order to clarify this ambiguity.