Testing Batch Apex

Batch Apex can be tested like any Apex code, although you are limited to a single transaction’s worth of data (one invocation of the execute method). A batch job started within a test runs synchronously, and does not count against the organization’s limit of five batch jobs.

The class in Listing 9.7 tests the Batch Apex example from Listing 9.1 and achieves 100% test coverage. The annotation IsTest(SeeAllData=true) allows the test to access the data in the organization rather than requiring it to create its own test data. Alternatively, you could modify the code to omit the annotation and insert a few Project records to serve as test data.

Listing 9.7 Batch Apex Test

@IsTest(SeeAllData=true) ...

Get Development with the Force.com Platform: Building Business Applications in the Cloud, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.