Stub method configuration
Mock objects have the ability to customize the return values for specific methods. Instead of returning null
by default, you can configure the mock object methods to return a specific value, something that you'll find useful in your unit tests to check for specific edge cases when calling other objects (dependencies).
These also have the ability to define expectations on the methods called, to check if the right methods were called with the expected parameters or if they were called in the right order, and so on. This way, we don't need to actually test an external dependent class, but we can define a contract
between the tested object and the dependencies.
Mock object methods can be configured to act as stub methods:
Stub methods will return null
by default, but you can configure the return value using the will()
method. They are defined in the setMethods()
array, or you don't call setMethods()
or call it using array()
.
Getting ready
In this recipe,...