Writing tests and the Selector layer

When writing tests specifically for a Selector class method, the process is almost as per the standard Apex testing guidelines; insert the data you need to support the queries, call the Selector methods, and assert the results returned. You can review some of the Selector tests included in this chapter.

If you have implemented Selector methods that return QueryLocator instances (for use by Batch Apex callers), you can still assert the results of these methods. Use the iterator method to obtain QueryLocatorIterator, and then call the next and hasNext methods to return the results to pass to your assert statements.

Of course, other Apex tests around the Controllers, Batch Apex, Domain, and Service layers may also invoke the Selector methods indirectly, thus providing code coverage, but not necessarily testing every aspect of the Selector classes.

Try to ensure that your Apex tests test the layers and functional components of the application as much as possible in isolation to ensure that the existing and future callers don't run into issues.

As highlighted previously, often writing more isolated Apex tests can be made harder due to the fact that the code being tested often requires records to be set up in the database. By leveraging the Selector factory option described earlier, a mocking approach can be taken to the Selector methods, which can help make writing more isolated and varied component or class-level tests easier. We will revisit this topic in Chapter 12, Unit Testing.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.152.159