SlideShare a Scribd company logo
1 of 6
Download to read offline
SFDCPanther
Testing Apex @cloudyamit
sfdcpanther@gmail.com
Agenda
❖ What is Test class in salesforce?
❖ What Components needs to be test?
❖ Syntax of basic test class and test method
❖ Test class best practices
What is test class in Salesforce
❖ A test class in Salesforce is also an Apex Class which is used to test the logic for Apex
trigger, Apex Class, VF Controller, VF Extensions, Batch Apex, Queueable Apex, future
apex.
❖ Test class insures that every logic is working as expected in both Positive & Negative
Testing.
❖ Test class code does not count in overall code coverage.
❖ Every Apex Class must have at least 75% code coverage before deploying into production
environment.
❖ Every Apex Trigger must need to have at least 1% code coverage.
❖ VF, VF Component, Lightning Component does not require any test class to migrate to
Production Environment.
!3
What component needs to be tested
❖ Apex Trigger
❖ Apex Class
❖ Batch Apex
❖ VF Controller/Extensions
❖ Future/Queueable Apex
❖ Restricted User
Write Basic Apex Test class
@isTest
private class MyTestClass {
@isTest static void myTest() {
// code_block
}
}
Test Class Best Practices
1. Use @isTest at the Top for all the test classes
2. Always put assert statements for negative and positive tests
3. Use the @testSetup method to insert the test data into Test class that will flow data all over the test class.
4. Use Utility Class to prepare the test data if same data is used in multiple test classes.
5. Always use Test.startTest() and Test.stopTest() doing this it will increase the governor limit of salesforce. We also
use this to increase the governor limit.
6. Use System.runAs() method to test the functionality in user Context.
7. Do not put (seeAllData = true) in test class otherwise, use it for exceptional cases.
8. Avoid Using Hard Coding Ids anywhere in test Class or any apex class.
9. Please make sure that each class has minimum 75% coverage and also the main functionality has been covered.
10. All class method must be tested in bulk way (for at least 200 records) and keep the real scenarios in mind.
11. Only one Test.startTest() and Test.stopTest() statement can be in a method, and no of Test.startTest() and
Test.stopTest() statement in any test class depend upon the test methods.

More Related Content

What's hot

What's hot (20)

Batch Apex in Salesforce
Batch Apex in SalesforceBatch Apex in Salesforce
Batch Apex in Salesforce
 
Introduction to Visualforce
Introduction to VisualforceIntroduction to Visualforce
Introduction to Visualforce
 
Salesforce sharing and visibility Part 1
Salesforce sharing and visibility Part 1Salesforce sharing and visibility Part 1
Salesforce sharing and visibility Part 1
 
Best Practices for RESTful Web Services
Best Practices for RESTful Web ServicesBest Practices for RESTful Web Services
Best Practices for RESTful Web Services
 
Deep Dive into Apex Triggers
Deep Dive into Apex TriggersDeep Dive into Apex Triggers
Deep Dive into Apex Triggers
 
Dive Deep into Apex: Advanced Apex!
Dive Deep into Apex: Advanced Apex! Dive Deep into Apex: Advanced Apex!
Dive Deep into Apex: Advanced Apex!
 
Two-Way Integration with Writable External Objects
Two-Way Integration with Writable External ObjectsTwo-Way Integration with Writable External Objects
Two-Way Integration with Writable External Objects
 
Episode 19 - Asynchronous Apex - Batch apex & schedulers
Episode 19 - Asynchronous Apex - Batch apex & schedulersEpisode 19 - Asynchronous Apex - Batch apex & schedulers
Episode 19 - Asynchronous Apex - Batch apex & schedulers
 
Practical Headless Flow Examples
Practical Headless Flow ExamplesPractical Headless Flow Examples
Practical Headless Flow Examples
 
REST API in Salesforce
REST API in SalesforceREST API in Salesforce
REST API in Salesforce
 
Selenium with java
Selenium with javaSelenium with java
Selenium with java
 
Asynchronous apex
Asynchronous apexAsynchronous apex
Asynchronous apex
 
Introduction to Apex Triggers
Introduction to Apex TriggersIntroduction to Apex Triggers
Introduction to Apex Triggers
 
Apex collection patterns
Apex collection patternsApex collection patterns
Apex collection patterns
 
Salesforce Sharing Architecture
Salesforce Sharing ArchitectureSalesforce Sharing Architecture
Salesforce Sharing Architecture
 
Build Reliable Asynchronous Code with Queueable Apex
Build Reliable Asynchronous Code with Queueable ApexBuild Reliable Asynchronous Code with Queueable Apex
Build Reliable Asynchronous Code with Queueable Apex
 
Data Management and Migration in Salesforce
Data Management and Migration in SalesforceData Management and Migration in Salesforce
Data Management and Migration in Salesforce
 
Salesforce asynchronous apex
Salesforce asynchronous apexSalesforce asynchronous apex
Salesforce asynchronous apex
 
Apex Trigger Debugging: Solving the Hard Problems
Apex Trigger Debugging: Solving the Hard ProblemsApex Trigger Debugging: Solving the Hard Problems
Apex Trigger Debugging: Solving the Hard Problems
 
Flow in Salesforce
Flow in SalesforceFlow in Salesforce
Flow in Salesforce
 

Similar to All About Test Class in #Salesforce

Testclass [Autosaved]
Testclass [Autosaved]Testclass [Autosaved]
Testclass [Autosaved]
Suraj Singh
 
Unit testing php-unit - phing - selenium_v2
Unit testing   php-unit - phing - selenium_v2Unit testing   php-unit - phing - selenium_v2
Unit testing php-unit - phing - selenium_v2
Tricode (part of Dept)
 
UNIT TESTING PPT
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPT
suhasreddy1
 
Running and Developing Tests with the Apache::Test Framework
Running and Developing Tests with the Apache::Test FrameworkRunning and Developing Tests with the Apache::Test Framework
Running and Developing Tests with the Apache::Test Framework
webhostingguy
 

Similar to All About Test Class in #Salesforce (20)

Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit Testing
 
Unit Testng with PHP Unit - A Step by Step Training
Unit Testng with PHP Unit - A Step by Step TrainingUnit Testng with PHP Unit - A Step by Step Training
Unit Testng with PHP Unit - A Step by Step Training
 
Unit testing in Force.com platform
Unit testing in Force.com platformUnit testing in Force.com platform
Unit testing in Force.com platform
 
Testclass [Autosaved]
Testclass [Autosaved]Testclass [Autosaved]
Testclass [Autosaved]
 
Unit testing php-unit - phing - selenium_v2
Unit testing   php-unit - phing - selenium_v2Unit testing   php-unit - phing - selenium_v2
Unit testing php-unit - phing - selenium_v2
 
An introduction to apex code test methods developer.force
An introduction to apex code test methods   developer.forceAn introduction to apex code test methods   developer.force
An introduction to apex code test methods developer.force
 
Jasmine framework
Jasmine frameworkJasmine framework
Jasmine framework
 
Episode 5 - Writing unit tests in Salesforce
Episode 5 - Writing unit tests in SalesforceEpisode 5 - Writing unit tests in Salesforce
Episode 5 - Writing unit tests in Salesforce
 
unit 1 (1).pptx
unit 1 (1).pptxunit 1 (1).pptx
unit 1 (1).pptx
 
UNIT TESTING PPT
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPT
 
.Net Unit Testing with Visual Studio 2010
.Net Unit Testing with Visual Studio 2010.Net Unit Testing with Visual Studio 2010
.Net Unit Testing with Visual Studio 2010
 
Dev labs alliance top 20 testng interview questions for sdet
Dev labs alliance top 20 testng interview questions for sdetDev labs alliance top 20 testng interview questions for sdet
Dev labs alliance top 20 testng interview questions for sdet
 
Unit testing
Unit testingUnit testing
Unit testing
 
Java Unit Test - JUnit
Java Unit Test - JUnitJava Unit Test - JUnit
Java Unit Test - JUnit
 
Test driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practicesTest driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practices
 
TestNG - The Next Generation of Unit Testing
TestNG - The Next Generation of Unit TestingTestNG - The Next Generation of Unit Testing
TestNG - The Next Generation of Unit Testing
 
Running and Developing Tests with the Apache::Test Framework
Running and Developing Tests with the Apache::Test FrameworkRunning and Developing Tests with the Apache::Test Framework
Running and Developing Tests with the Apache::Test Framework
 
Unit testing
Unit testingUnit testing
Unit testing
 
Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.
 
TestNG Session presented in PB
TestNG Session presented in PBTestNG Session presented in PB
TestNG Session presented in PB
 

More from Amit Singh

More from Amit Singh (18)

CI CD Daridabad MuleSoft meetup
CI CD Daridabad MuleSoft meetupCI CD Daridabad MuleSoft meetup
CI CD Daridabad MuleSoft meetup
 
Custom MuleSoft connector using Java SDK
Custom MuleSoft connector using Java SDKCustom MuleSoft connector using Java SDK
Custom MuleSoft connector using Java SDK
 
External services low code integration
External services   low code integrationExternal services   low code integration
External services low code integration
 
Faridabad Mulesoft Meetup Oct 10
Faridabad Mulesoft Meetup Oct 10Faridabad Mulesoft Meetup Oct 10
Faridabad Mulesoft Meetup Oct 10
 
Error Handling in Mulesoft
Error Handling in MulesoftError Handling in Mulesoft
Error Handling in Mulesoft
 
Mule Testing in Mulesfoft 4.X
Mule Testing in Mulesfoft 4.XMule Testing in Mulesfoft 4.X
Mule Testing in Mulesfoft 4.X
 
Test setup methods in Salesforce Apex Class
Test setup methods in Salesforce Apex ClassTest setup methods in Salesforce Apex Class
Test setup methods in Salesforce Apex Class
 
Apex trigger handler & helper
Apex trigger handler & helperApex trigger handler & helper
Apex trigger handler & helper
 
Rollup Summary Trigger for Lookup Relationship Salesforce #Salesforce #ApexTr...
Rollup Summary Trigger for Lookup Relationship Salesforce #Salesforce #ApexTr...Rollup Summary Trigger for Lookup Relationship Salesforce #Salesforce #ApexTr...
Rollup Summary Trigger for Lookup Relationship Salesforce #Salesforce #ApexTr...
 
Duplicate Contact Trigger || Trigger Logic Building || #ApexTrigger #Salesforce
Duplicate Contact Trigger || Trigger Logic Building || #ApexTrigger #SalesforceDuplicate Contact Trigger || Trigger Logic Building || #ApexTrigger #Salesforce
Duplicate Contact Trigger || Trigger Logic Building || #ApexTrigger #Salesforce
 
OAuth 2.0 an Overview
OAuth 2.0 an OverviewOAuth 2.0 an Overview
OAuth 2.0 an Overview
 
Salesforce apex hours PayPal with Salesforce Integration
Salesforce apex hours   PayPal with Salesforce IntegrationSalesforce apex hours   PayPal with Salesforce Integration
Salesforce apex hours PayPal with Salesforce Integration
 
SOQL in salesforce || Salesforce Object Query Language || Salesforce
SOQL in salesforce || Salesforce Object Query Language || SalesforceSOQL in salesforce || Salesforce Object Query Language || Salesforce
SOQL in salesforce || Salesforce Object Query Language || Salesforce
 
Introduction to apex trigger context variables
Introduction to apex trigger context variablesIntroduction to apex trigger context variables
Introduction to apex trigger context variables
 
Introduction to apex triggers
Introduction to apex triggersIntroduction to apex triggers
Introduction to apex triggers
 
Salesforce apex hours azure dev ops
Salesforce apex hours   azure dev opsSalesforce apex hours   azure dev ops
Salesforce apex hours azure dev ops
 
Lightning web components
Lightning web componentsLightning web components
Lightning web components
 
Salesforce apex hours azure active directory seamless single sign-on with s...
Salesforce apex hours   azure active directory seamless single sign-on with s...Salesforce apex hours   azure active directory seamless single sign-on with s...
Salesforce apex hours azure active directory seamless single sign-on with s...
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 

All About Test Class in #Salesforce

  • 2. Agenda ❖ What is Test class in salesforce? ❖ What Components needs to be test? ❖ Syntax of basic test class and test method ❖ Test class best practices
  • 3. What is test class in Salesforce ❖ A test class in Salesforce is also an Apex Class which is used to test the logic for Apex trigger, Apex Class, VF Controller, VF Extensions, Batch Apex, Queueable Apex, future apex. ❖ Test class insures that every logic is working as expected in both Positive & Negative Testing. ❖ Test class code does not count in overall code coverage. ❖ Every Apex Class must have at least 75% code coverage before deploying into production environment. ❖ Every Apex Trigger must need to have at least 1% code coverage. ❖ VF, VF Component, Lightning Component does not require any test class to migrate to Production Environment. !3
  • 4. What component needs to be tested ❖ Apex Trigger ❖ Apex Class ❖ Batch Apex ❖ VF Controller/Extensions ❖ Future/Queueable Apex ❖ Restricted User
  • 5. Write Basic Apex Test class @isTest private class MyTestClass { @isTest static void myTest() { // code_block } }
  • 6. Test Class Best Practices 1. Use @isTest at the Top for all the test classes 2. Always put assert statements for negative and positive tests 3. Use the @testSetup method to insert the test data into Test class that will flow data all over the test class. 4. Use Utility Class to prepare the test data if same data is used in multiple test classes. 5. Always use Test.startTest() and Test.stopTest() doing this it will increase the governor limit of salesforce. We also use this to increase the governor limit. 6. Use System.runAs() method to test the functionality in user Context. 7. Do not put (seeAllData = true) in test class otherwise, use it for exceptional cases. 8. Avoid Using Hard Coding Ids anywhere in test Class or any apex class. 9. Please make sure that each class has minimum 75% coverage and also the main functionality has been covered. 10. All class method must be tested in bulk way (for at least 200 records) and keep the real scenarios in mind. 11. Only one Test.startTest() and Test.stopTest() statement can be in a method, and no of Test.startTest() and Test.stopTest() statement in any test class depend upon the test methods.