Testing the Service layer
It is tempting to allow the testing of the Service layer to be done via tests around the calling code, such as Visualforce controller tests. However, depending solely on this type of testing leaves the Service layer logic open to other use cases that may not strictly be covered by the controller logic. For example, a custom controller using StandardController
will only pass in a single record and not multiple ones.
Make sure to develop specific Apex tests against the Service layer as the functionality is developed. Although outside the scope of this book, Service layer development is an ideal candidate for practicing Test Driven Development.
Mocking the Service layer
Sometimes, the data setup requirements of the Service layer are such that it makes writing Apex tests for controllers or other callers, such as Batch Apex, quite complex and thus expensive, not only in server time for the test to run (due to data setup for each test method) but also in developer time...