In writing unit tests, for each object that the unit interacts with, I am taking these steps (stolen from my understanding of JBrains\' Integration Tests are a Scam):
You should not trust human beings (even yourself) about keeping mock and real software components in synch.
I hear you ask?
Then what is your proposal?
My proposal is;
You should write mocks.
You should only write mocks for software components that you maintain.
If you maintain a software component with another developer, you and the other developer should maintain mock of that component together.
You should not mock someone else's component.
When you write a unit test for your component, you should write a separate unit test for the mock of that component. Let's call that MockSynchTest
.
In MockSynchTest
you should compare every behavior of the mock with the real component.
When you make changes to your component, you should run the MockSynchTest
to see if you made your mock and component out-of-synch or not.
If you need the mock of a component that you do not maintain while testing your components, ask the developer of that component about the mock. If she can provide you with a well tested mock, good for her and lucky for you. If she can not, kindly ask her to follow this guideline and provide you a well tested mock.
This way if you accidentally make your mock out of synch, there will be a failing test case there to warn you.
This way you do not need to know the implementation details of the foreign component for mocking.
How-to-write-good-tests#dont-mock-type-you-dont-own