Skip to main content
added 219 characters in body
Source Link

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 and Database 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 a Database which will be injected into the Service which will be injected into the Controller vs only mocking the Service which will be injected to the Controller.

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 and Database are tested
  • It is the same an integration test
  • Setup is harder since the Controller depend everything

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 and Database 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 a Database which will be injected into the Service which will be injected into the Controller vs only mocking the Service which will be injected to the Controller.
Source Link

Should I unit test "internal" classes?

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 and Database are tested
  • It is the same an integration test
  • Setup is harder since the Controller depend everything