Part IV. 5. Test Class Creation in Salesforce



Writing Test Class in Salesforce

The Apex testing framework enables you to write and execute tests for your Apex classes and triggers on the Lightning Platform. Apex unit tests ensure high quality for your Apex code and let you meet requirements for deploying Apex.

Testing is the key to successful long-term development and is a critical component of the development process. The Apex testing framework makes it easy to test your Apex code. Apex code can only be written in a sandbox environment or a Developer org, not in production.

Apex code can be deployed to a production org from a sandbox. Also, app developers can distribute Apex code to customers from their Developer orgs by uploading packages to the Lightning Platform​ AppExchange.

In addition to being critical for quality assurance, Apex unit tests are also requirements for deploying and distributing Apex.

The following are the benefits of Apex unit tests.

  • Ensuring that your Apex classes and triggers work as expected
  • Having a suite of regression tests that can be rerun every time classes and triggers are updated to ensure that future updates you make to your app don’t break existing functionality
  • Meeting the code coverage requirements for deploying Apex to production or distributing Apex to customers via packages
  • High-quality apps delivered to the production org, which makes production users more productive
  • High-quality apps delivered to package subscribers, which increase your customers trust.

Test Method Syntax

Test methods take no arguments and have the following syntax:

@isTest static void testName() {
// code_block
}

Alternatively, a test method can have this syntax:

static testMethod void testName() {
// code_block
}

Using the isTest annotation instead of the testMethod keyword is more flexible as you can specify parameters in the annotation.

Declaring a test method as public or private doesn’t make a difference as the testing framework is always able to access test methods. For this reason, the access modifiers are omitted in the syntax.

Test methods must be defined in test classes, which are classes annotated with isTest.

We have a sample scheduler class and Test class. Please try like the below,

Scheduler Class:

global class ScheduleTerritoryInactiveMemberCheck implements Schedulable { 
    global void execute(SchedulableContext SC) { 
    TerritoryInactiveMemberCheck bcc = new TerritoryInactiveMemberCheck(); 
    bcc.query = 'select id,name from territory'; 
    Database.executeBatch(bcc,200); 
    }
 
} 

Test class:
public static testMethod void testschedule() {
Test.StartTest();
ScheduleTerritoryInactiveMemberCheck sh1 = new ScheduleTerritoryInactiveMemberCheck();
String sch = '0 0 23 * * ?';
system.schedule('Test Territory Check', sch, sh1);
Test.stopTest();
}

We are the ISV Partners and Please reach us for custom development => www.merfantz.com

Please refer the following link to learn more about test class Writing Test class for batch class.

----------------------Hope this will help you!-------------------------


You must be logged into post a comment.