• WHO WE ARE
  • WHAT WE DO
    • Implementations
      • Salesforce
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service for SMEs
        • Field Service Lightning
      • Salesforce Partner Apps
        • Panda Doc
        • Conga
    • Developments
      • Salesforce Customization
      • Custom Application Development
      • AppExchange Product Development
    • Migrations
      • Classic to Lightning Migration
      • Other Systems to Salesforce Migration
    • Integrations
  • HOW WE DO
    • Delivery Model
    • Our Works
  • BLOG
    • WHO WE ARE
    • WHAT WE DO
      • Implementations
        • Salesforce
          • Sales Cloud
          • Service Cloud
          • CPQ
          • Field Service for SMEs
          • Field Service Lightning
        • Salesforce Partner Apps
          • Panda Doc
          • Conga
      • Developments
        • Salesforce Customization
        • Custom Application Development
        • AppExchange Product Development
      • Migrations
        • Classic to Lightning Migration
        • Other Systems to Salesforce Migration
      • Integrations
    • HOW WE DO
      • Delivery Model
      • Our Works
    • BLOG
Salesforce Blog | Salesforce Simple Tutorial
Salesforce Blog | Salesforce Simple Tutorial
  • WHO WE ARE
  • WHAT WE DO
    • Implementations
      • Salesforce
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service for SMEs
        • Field Service Lightning
      • Salesforce Partner Apps
        • Panda Doc
        • Conga
    • Developments
      • Salesforce Customization
      • Custom Application Development
      • AppExchange Product Development
    • Migrations
      • Classic to Lightning Migration
      • Other Systems to Salesforce Migration
    • Integrations
  • HOW WE DO
    • Delivery Model
    • Our Works
  • BLOG

Part IV. 5. Test Class Creation in Salesforce

Salesforce Blog | Salesforce Simple Tutorial > SFDC Training > SFDC Training Part IV > Part IV. 5. Test Class Creation in Salesforce
  • November 2, 2017
  • Merfantz Editor
  • SFDC Training, SFDC Training Part IV
  • 0

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!-------------------------


  • Previous Part IV. 4. Trigger Creation in Salesforce
  • Next Part IV. 6. Components Creation in Salesforce

Leave a Reply

You must be logged in to post a comment.

We denounce with righteous indignation in and dislike men who are so beguiled and to demo realized by the charms of pleasure moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound.

Discover More

Contact Info

  • 374 William S Canning Blvd, Fall River MA 2721, USA
  • (+880)155-69569
  • [email protected]
  • 10:00 - 17:00

Latest Posts

Salesforce Marketing Cloud Will Fuel Your B2B Marketing Engine. January 30, 2023
Overview of DML operations in Apex January 24, 2023
Benefits of Integrating Salesforce and Marketing Automation for Business Growth January 20, 2023

2023 All Rights Reserved