Adding a Test for the Batch Apex Class | Techila
1096
single,single-post,postid-1096,single-format-standard,ajax_updown_fade,page_not_loaded,,qode_grid_1300,footer_responsive_adv,hide_top_bar_on_mobile_header,qode-content-sidebar-responsive,qode-theme-ver-9.4.2,bridge,wpb-js-composer js-comp-ver-4.12,vc_responsive
 

Adding a Test for the Batch Apex Class

TEST FOR THE BATCH APEX CLASS

Adding a Test for the Batch Apex Class

In this blog, you’ll add a test class for the CleanUpRecords batch class. The test in this class invokes the batch job and verifies that it deletes all merchandise records that haven’t been purchased.

1.      Click File | New | Apex Class.

2.      For the class name, enter TestCleanUpBatchClass and click OK.

3.      Delete the auto-generated code and add the following.

@isTest

private class TestCleanUpBatchClass {

  static testmethod void test() {

 // The query used by the batch job.

 String query = 'SELECT Id,CreatedDate FROM Merchandise__c ' +

'WHERE Id NOT IN (SELECT Merchandise__c FROM Line_Item__c)';

// Create some test merchandise items to be deleted

//   by the batch job.

   Merchandise__c[] ml = new List<Merchandise__c>();

for (Integer i=0;i<10;i++) {

 Merchandise__c m = new Merchandise__c(

 Name='Merchandise' + i, Description__c='Some description', Price__c=2, Total_Inventory__c=100);

 ml.add(m);

 }

insert ml;

  Test.startTest();

CleanUpRecords c = new CleanUpRecords(query);

Database.executeBatch(c);

 Test.stopTest();

 // Verify merchandise items got deleted

 Integer i = [SELECT COUNT() FROM Merchandise__c];

  System.assertEquals(i, 0);

 }

 }

       4.      Click File | Save.

  1. The test class contains one test method called test. This test method starts by constructing the query string that is to be passed to the constructor of CleanUpRecords. Since a merchandise item that hasn’t been purchased is a merchandise item that doesn’t have line items associated with it, the SOQL query specifies the following:

WHERE Id NOT IN (SELECT Merchandise__c FROM Line_Item__c)

  1. The subquery

SELECT Merchandise__c FROM Line_Item__c

  1. gets the set of all merchandise items that are referenced in line items. Since the query uses the NOT IN operator in the WHERE clause, this means the merchandise items that aren’t referenced in line items are returned.
  2. The test method inserts 10 merchandise items with no associated line items to be cleaned up by the batch class method. Note that the number of records inserted is less than the batch size of 200 because test methods can execute only one batch total.
  3. Next, the batch class is instantiated with the query with this statement where the query variable is passed to the constructor of CleanUpRecords.

CleanUpRecords c = new CleanUpRecords(query);

  1. The batch class is invoked by calling Database.executeBatch and passing it the instance of the batch class:

Database.executeBatch(c);

  1. The call to Database.executeBatch is included within the Test.startTest and Test.stopTest block. This is necessary for the batch job to run in a test method. The job executes after the call to Test.stopTest. Any asynchronous code included within Test.startTest and Test.stopTest gets executed synchronously after Test.stopTest.
  2. Finally, the test verifies that all test merchandise items created in this test got deleted by checking that the count of merchandise items is zero.
  3. Even though the batch class finish method sends a status email message, the email isn’t sent in this case because email messages don’t get sent from test methods.

 

Courtesy- https://developer.salesforce.com/docs/atlas.en-us.apex_workbook.meta/apex_workbook/apex_batch_2.htm

https://trailhead.salesforce.com/modules/apex_testing

 

“Know more about Salesforce Consulting at Techila”

Author: techila

No Comments

Post A Comment