I have been using TDD when developing some of my side projects and have been loving it.
The issue, however, is that stubbing classes for unit tests are a pain and makes you afraid of refactoring.
I started researching and I see that there are a group of people that advocates for TDD without mocking–the classicists, if I am not mistaken.
However, how would I go about writing unit tests for a piece of code that uses one or more dependencies? For instance, if I am testing a UserService
class that needs UserRepository
(talks to the database) and UserValidator
(validates the user), then the only way would be… to stub them?
Otherwise, if I use a real UserRepository
and UserValidator
, wouldn’t that be an integration test and also defeat the purpose of testing only the behavior of UserService
?
Should I be writing only integration tests when there is dependency, and unit tests for pieces of code without any dependency?
And if so, how would I test the behavior of UserService
? ("If UserRepository
returns null, then UserService
should return false", etc.)
Thank you.