I look at it this way: the "unit" you are testing here is the CompanyService
. In this sense all of your tests look like unit tests to me. Underneath your service, though, there may be another service (you mention a database) that this test is also exercising? This could start to blur the lines with integration testing a bit, but you have to ask yourself if it matters. You could stub out any such underlying service, and you may want to if:
- The underlying service is slow to set up or use, making your unit tests too slow.
- You want to be sure the behaviour of this test is unaffected by the underlying service - i.e. this test should only fail if there is a bug in
CompanyService
.
In my experience, provided the underlying service is fast enough I don't worry too much about my unit test relying on it. I don't mind a bit of integration leaking into my unit tests, as it has benefits (more integration coverage) and rarely causes a problem. If it does cause problems you can always come back to it and add stubbing to improve the isolation.