I am asking about mock object management, regardless the specific implementation (EasyMock, Mock Object etc).
I have been reluctant to use Mock object in my unit testings, for the following reason: The behavior of Mock object must mirror the behavior of object being mocked. If the behavior of the object being mocked has changed, we will have to change behavior of mock object as well. If we don't, the behavior of mock object will out of sync with the real object, and thus make the unit-testing meaningless, and it's dangerous.
My question are, how to keep the mock object in sync with target object? How do you propagate the changes? Do you use any mock object management technique?
Edit: Change title to narrow down the scope.
Stubbing, like mocking, means creating a stand-in, but a stub only mocks the behavior, but not the entire object. This is used when your implementation only interacts with a certain behavior of the object.
What happens when you mock something? First of all, we should define what a mock is: In a unit test, mock objects can simulate the behavior of complex, real objects and are therefore useful when it is impractical or impossible to incorporate a real object into a unit test. Mocking makes sense in a unit testing context.
Using mock objects allows developers to focus their tests on the behavior of the system under test without worrying about its dependencies. For example, testing a complex algorithm based on multiple objects being in particular states can be clearly expressed using mock objects in place of real objects.
Both can be used to mock methods or fields. The difference is that in mock, you are creating a complete mock or fake object while in spy, there is the real object and you just spying or stubbing specific methods of it. When using mock objects, the default behavior of the method when not stub is do nothing.
Well defined APIs should not have this sort of leeway: given a set of input, the object being mocked should only behave in these particular ways: the behavior is tied to the interface. If there is allowed variance, then your mock object should be testing all of the different things this object could do.
You can mitigate the risk of behavior drift by:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With