Why do we mock dependencies when unit testing
Wether you are testing a Java Spring Boot service or an Angular service, I hope you don't actually call the services and resources that your service depends on.
That is either the repository/database layer (DAO layer) and/or the actual http service call for Java and Angular respectively. Instead, we usually mock this backend resource.
I don't want to go into the detail of how to do that today. Here in (angular.io)[https://angular.io/guide/testing-services] you get a documentation on how to test angular services with Jasmine. For Spring Services I usually use mockito and here is (a good tutorial on how to mock underlaying services) [https://howtodoinjava.com/spring-boot2/testing/spring-boot-mockito-junit-example/]
But, I want to pause and ponder why we do the mocking or stubbing in the first place. Because sometimes I see a mock and an actual services call. Which happens due to completely missing the point.
The reason we write mocks
The reason we mock other services that we are dependent on, and DAO layer objects is because this is unit test. And the main idea behind unit testing is to scrutinize our methods which are supposdly the smallest testable part of our code.
In fact, this is possible if we write a code with separation of concern in mind. With small methods that are testable. Once we accomplish that we can test the logic in that method. And since we are going for testing only the logic in that method, we can agree to ignore the other dependencies. That is why it is important not to actually call these other services. That is why there is a need to mock it.
Why mocking is important
Mocking is important becuase it enables us to isolate that tiny method we are trying to put under the microscope.
I think if we have this in mind, our TestBed will inherently look better. So would our methods.
Unit tests are for us
Also, let's just remind ourservels that we write unit tests for our own purpose. Not for the tech lead, the PM or the scrum master. If the tech lead is spending time fixing people's unit test weekly then there is a bigger problem. He/she doesn't get the idea too.
There should no a need to do that.