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
Serviceclass 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
ServiceandDatabaseare tested - It is the same an integration test
- Setup is harder since the
Controllerdepend everything. By harder I mean that I will have to create aDatabasewhich will be injected into theServicewhich will be injected into theControllervs only mocking theServicewhich will be injected to theController.
ServiceandDatabaseclasses as internal, while some developers would simply call them a class.Servicenor aDatabaseclass is an implementation detail of aControllerclass, 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.