Unit testing of software components requires that a component is tested in isolation. However, many components require the services of other components to provide their functionality. In order to decouple the component being tested from its dependencies, one commonly uses mock objects. Mocking frameworks which are currently used in industry use dynamic mock object generation with the mocking behaviour specified where the mocking is required, e.g. within unit tests. There is no guarantee that the mocking behaviour is consistent with the behaviour required from the component being mocked. Furthermore, these mock objects are not reused across unit tests and other areas where mocking is required.   We consider an alternative approach to mocking where the mock object is specified against the contract being mocked and reused across unit tests and other code where mocking is required. These mock objects are tested against the component contracts for the components they are mocking. Furthermore, using dependency injection one is able to reuse the functional tests across unit tests (injecting mock objects for the dependencies) and integration tests (injecting actual components for the dependencies). Even though this approach does require more code to be developed for specifying mocking behaviour, this is offset by the mock object being reused across unit tests for components with the same dependency as well as across integration tests.
['Fritz Solms', 'Linda Marshall']
Contract-Based Mocking for Services-Oriented Development