SlideShare una empresa de Scribd logo
1 de 8
Test Classes - Salesforce
LEARN . SHARE . CELEBRATE . SALESFORCE
Why Test Classes
• Error-free code
• Check whether code is working as expected or not
• Deploy Apex to a production environment
• Salesforce requires at least 75% of your code to be covered by
our test classes
• Cover each positive and negative use case of your code
Code Coverage Calculation
• Code coverage is calculated by dividing the number of unique
Apex code lines executed during your test method execution by
the total number of Apex code lines in all of your trigger and
classes.
(Note: these numbers do not include lines of code within your
testMethods)
Considerations
• System.debug are not part of Apex code coverage.
• Test methods and test classes are not counted as part of Apex
code limit.
• Every trigger you are trying to deploy should have at least 1%
coverage
• Class can be deployed on 0% coverage but yes overall coverage
of your production org after getting your code deployed should be
75%, otherwise Salesforce won’t let you deploy your code.
• Strive for 100% code coverage. Do not focus on the 75%
requirement.
• Test methods do not expect any Id or rely upon a specific data
set. Test method will fail when deploy to other organisation.
• System.assertEquals
Test Method Syntax and Static Methods
• Static testMethod keyword
• @IsTest : Define classes that only contain code used for testing your
application
• Test.isRunningTest() : Returns true if the currently executing code was called
by code contained in a test method, false otherwise
• Test.startTest : Test actually begins. Initialize variables, setup data before it.
Get a fresh set of governor limits for the remainder of the test until you call
Test.stopTest
• System.RunAs : Change user contexts to either an existing user or a new user.
All of that user's record sharing is then enforced
• isTest(SeeAllData = true) : Use on class methods in exceptional cases where
there are sObjects that doesn't allow DML operation e.g. PriceBook creation
Example
@isTest
private class MyTestClass {
@isTest static void test1(){
// Implement test code
}
}
Questions
Follow Seema Dhingra
Seema Dhingra
Associate Manager Accenture
Blog : https://salesforcecodelearn.blogsopt.in
Twitter : @sfdc_Seema
Facebook Page : @salesforcecodelearn

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

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
 
Lwc presentation
Lwc presentationLwc presentation
Lwc presentation
 
Apex trigger framework Salesforce #ApexTrigger #Salesforce #SFDCPanther
Apex trigger framework Salesforce #ApexTrigger #Salesforce #SFDCPantherApex trigger framework Salesforce #ApexTrigger #Salesforce #SFDCPanther
Apex trigger framework Salesforce #ApexTrigger #Salesforce #SFDCPanther
 
SFDC Batch Apex
SFDC Batch ApexSFDC Batch Apex
SFDC Batch Apex
 
Salesforce asynchronous apex
Salesforce asynchronous apexSalesforce asynchronous apex
Salesforce asynchronous apex
 
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
 
Deep Dive into Apex Triggers
Deep Dive into Apex TriggersDeep Dive into Apex Triggers
Deep Dive into Apex Triggers
 
Migrate To Lightning Web Components from Aura framework to increase performance
Migrate To Lightning Web Components from Aura framework to increase performance Migrate To Lightning Web Components from Aura framework to increase performance
Migrate To Lightning Web Components from Aura framework to increase performance
 
Introduction to Apex Triggers
Introduction to Apex TriggersIntroduction to Apex Triggers
Introduction to Apex Triggers
 
Salesforce Integration
Salesforce IntegrationSalesforce Integration
Salesforce Integration
 
Triggers and order of execution1
Triggers and order of execution1Triggers and order of execution1
Triggers and order of execution1
 
Introduction to Apex Triggers
Introduction to Apex TriggersIntroduction to Apex Triggers
Introduction to Apex Triggers
 
Asynchronous Apex Salesforce World Tour Paris 2015
Asynchronous Apex Salesforce World Tour Paris 2015Asynchronous Apex Salesforce World Tour Paris 2015
Asynchronous Apex Salesforce World Tour Paris 2015
 
Salesforce Basic Development
Salesforce Basic DevelopmentSalesforce Basic Development
Salesforce Basic Development
 
Styled Components & React.js
Styled Components & React.jsStyled Components & React.js
Styled Components & React.js
 
Best Practices with Apex in 2022.pdf
Best Practices with Apex in 2022.pdfBest Practices with Apex in 2022.pdf
Best Practices with Apex in 2022.pdf
 
Asynchronous apex
Asynchronous apexAsynchronous apex
Asynchronous apex
 
Managing Your Batch and Scheduled Apex Processes with Relax
Managing Your Batch and Scheduled Apex Processes with RelaxManaging Your Batch and Scheduled Apex Processes with Relax
Managing Your Batch and Scheduled Apex Processes with Relax
 
Getting Started With Apex REST Services
Getting Started With Apex REST ServicesGetting Started With Apex REST Services
Getting Started With Apex REST Services
 
Salesforce customization vs configuration
Salesforce customization vs configurationSalesforce customization vs configuration
Salesforce customization vs configuration
 

Similar a Test Classes in Salesforce

Testclass [Autosaved]
Testclass [Autosaved]Testclass [Autosaved]
Testclass [Autosaved]
Suraj Singh
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
Meilan Ou
 
Unit Testing Best Practices
Unit Testing Best PracticesUnit Testing Best Practices
Unit Testing Best Practices
Tomaš Maconko
 

Similar a Test Classes in Salesforce (20)

Unit testing in Force.com platform
Unit testing in Force.com platformUnit testing in Force.com platform
Unit testing in Force.com platform
 
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 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
 
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
 
Testclass [Autosaved]
Testclass [Autosaved]Testclass [Autosaved]
Testclass [Autosaved]
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
 
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
 
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
Agile Mumbai 2020 Conference |  How to get the best ROI on Your Test Automati...Agile Mumbai 2020 Conference |  How to get the best ROI on Your Test Automati...
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
 
Junit
JunitJunit
Junit
 
Code coverage
Code coverageCode coverage
Code coverage
 
Database Unit Testing Made Easy with VSTS
Database Unit Testing Made Easy with VSTSDatabase Unit Testing Made Easy with VSTS
Database Unit Testing Made Easy with VSTS
 
unit 1 (1).pptx
unit 1 (1).pptxunit 1 (1).pptx
unit 1 (1).pptx
 
SF1 - Apex Development Best Practises
SF1 - Apex Development Best PractisesSF1 - Apex Development Best Practises
SF1 - Apex Development Best Practises
 
Microsoft Fakes, Unit Testing the (almost) Untestable Code
Microsoft Fakes, Unit Testing the (almost) Untestable CodeMicrosoft Fakes, Unit Testing the (almost) Untestable Code
Microsoft Fakes, Unit Testing the (almost) Untestable Code
 
Unit Tests with Microsoft Fakes
Unit Tests with Microsoft FakesUnit Tests with Microsoft Fakes
Unit Tests with Microsoft Fakes
 
Java Unit Test - JUnit
Java Unit Test - JUnitJava Unit Test - JUnit
Java Unit Test - JUnit
 
Unit tests and TDD
Unit tests and TDDUnit tests and TDD
Unit tests and TDD
 
Testing, a pragmatic approach
Testing, a pragmatic approachTesting, a pragmatic approach
Testing, a pragmatic approach
 
Unit Testing Best Practices
Unit Testing Best PracticesUnit Testing Best Practices
Unit Testing Best Practices
 
Testing Frameworks
Testing FrameworksTesting Frameworks
Testing Frameworks
 

Más de Atul Gupta(8X)

Meetup with kavindra : New Delhi Salesforce Developer Group
Meetup with kavindra : New Delhi Salesforce Developer GroupMeetup with kavindra : New Delhi Salesforce Developer Group
Meetup with kavindra : New Delhi Salesforce Developer Group
Atul Gupta(8X)
 

Más de Atul Gupta(8X) (20)

Developer Week 2019 Delhi + Spring 19 Features
Developer Week 2019 Delhi + Spring 19 FeaturesDeveloper Week 2019 Delhi + Spring 19 Features
Developer Week 2019 Delhi + Spring 19 Features
 
Engagement Studio, Pardot at Developer Week 2019
Engagement Studio, Pardot at Developer Week 2019Engagement Studio, Pardot at Developer Week 2019
Engagement Studio, Pardot at Developer Week 2019
 
Dreamforce 2018 Global Gathering
Dreamforce 2018 Global Gathering Dreamforce 2018 Global Gathering
Dreamforce 2018 Global Gathering
 
Introduction to Salesforce UI API
Introduction to Salesforce UI APIIntroduction to Salesforce UI API
Introduction to Salesforce UI API
 
TrailheaDX Global Gathering: Agenda and Introduction
TrailheaDX Global Gathering: Agenda and IntroductionTrailheaDX Global Gathering: Agenda and Introduction
TrailheaDX Global Gathering: Agenda and Introduction
 
Building Next-Gen Communities With Lightning Series : Session#01 : Virtual #S...
Building Next-Gen Communities With Lightning Series : Session#01 : Virtual #S...Building Next-Gen Communities With Lightning Series : Session#01 : Virtual #S...
Building Next-Gen Communities With Lightning Series : Session#01 : Virtual #S...
 
Virtual #SalesforceSaturday : Salesforce Connect with Cross Org Adaptor
Virtual #SalesforceSaturday : Salesforce Connect with Cross Org AdaptorVirtual #SalesforceSaturday : Salesforce Connect with Cross Org Adaptor
Virtual #SalesforceSaturday : Salesforce Connect with Cross Org Adaptor
 
#SalesforceSaturday : Salesforce BIG Objects Explained
#SalesforceSaturday : Salesforce BIG Objects Explained#SalesforceSaturday : Salesforce BIG Objects Explained
#SalesforceSaturday : Salesforce BIG Objects Explained
 
#SalesforceSaturday Spring18 Release Highlights by Manish Thaduri
#SalesforceSaturday Spring18 Release Highlights by Manish Thaduri#SalesforceSaturday Spring18 Release Highlights by Manish Thaduri
#SalesforceSaturday Spring18 Release Highlights by Manish Thaduri
 
Success Story and Dreamforce Experience : Jaipur User Group DF17 Global Gathe...
Success Story and Dreamforce Experience : Jaipur User Group DF17 Global Gathe...Success Story and Dreamforce Experience : Jaipur User Group DF17 Global Gathe...
Success Story and Dreamforce Experience : Jaipur User Group DF17 Global Gathe...
 
Dreamforce Global Gathering : New Delhi Salesforce DG & Gurgaon WIT
Dreamforce Global Gathering : New Delhi Salesforce DG & Gurgaon WITDreamforce Global Gathering : New Delhi Salesforce DG & Gurgaon WIT
Dreamforce Global Gathering : New Delhi Salesforce DG & Gurgaon WIT
 
Developer to Consultant : Transformation Tips #SalesforceSaturday
Developer to Consultant : Transformation Tips #SalesforceSaturdayDeveloper to Consultant : Transformation Tips #SalesforceSaturday
Developer to Consultant : Transformation Tips #SalesforceSaturday
 
Spring 17 Salesforce Viewing Party : New Delhi Salesforce Developer Group
Spring 17 Salesforce Viewing Party : New Delhi Salesforce Developer GroupSpring 17 Salesforce Viewing Party : New Delhi Salesforce Developer Group
Spring 17 Salesforce Viewing Party : New Delhi Salesforce Developer Group
 
Meetup with kavindra : New Delhi Salesforce Developer Group
Meetup with kavindra : New Delhi Salesforce Developer GroupMeetup with kavindra : New Delhi Salesforce Developer Group
Meetup with kavindra : New Delhi Salesforce Developer Group
 
Marketing Cloud, SalesforceSaturday
Marketing Cloud, SalesforceSaturdayMarketing Cloud, SalesforceSaturday
Marketing Cloud, SalesforceSaturday
 
Live Agent Setup SalesforceSaturday
Live Agent Setup SalesforceSaturdayLive Agent Setup SalesforceSaturday
Live Agent Setup SalesforceSaturday
 
Data Loader Command Line Interface
Data Loader Command Line InterfaceData Loader Command Line Interface
Data Loader Command Line Interface
 
How to list your app on AppExchange
How to list your app on AppExchangeHow to list your app on AppExchange
How to list your app on AppExchange
 
Force.com Migration Tool
Force.com Migration ToolForce.com Migration Tool
Force.com Migration Tool
 
TrailheaDX Viewing Party - New Delhi Salesforce DG
TrailheaDX Viewing Party - New Delhi Salesforce DGTrailheaDX Viewing Party - New Delhi Salesforce DG
TrailheaDX Viewing Party - New Delhi Salesforce DG
 

Último

Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 

Último (20)

Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 

Test Classes in Salesforce

  • 1. Test Classes - Salesforce LEARN . SHARE . CELEBRATE . SALESFORCE
  • 2. Why Test Classes • Error-free code • Check whether code is working as expected or not • Deploy Apex to a production environment • Salesforce requires at least 75% of your code to be covered by our test classes • Cover each positive and negative use case of your code
  • 3. Code Coverage Calculation • Code coverage is calculated by dividing the number of unique Apex code lines executed during your test method execution by the total number of Apex code lines in all of your trigger and classes. (Note: these numbers do not include lines of code within your testMethods)
  • 4. Considerations • System.debug are not part of Apex code coverage. • Test methods and test classes are not counted as part of Apex code limit. • Every trigger you are trying to deploy should have at least 1% coverage • Class can be deployed on 0% coverage but yes overall coverage of your production org after getting your code deployed should be 75%, otherwise Salesforce won’t let you deploy your code. • Strive for 100% code coverage. Do not focus on the 75% requirement. • Test methods do not expect any Id or rely upon a specific data set. Test method will fail when deploy to other organisation. • System.assertEquals
  • 5. Test Method Syntax and Static Methods • Static testMethod keyword • @IsTest : Define classes that only contain code used for testing your application • Test.isRunningTest() : Returns true if the currently executing code was called by code contained in a test method, false otherwise • Test.startTest : Test actually begins. Initialize variables, setup data before it. Get a fresh set of governor limits for the remainder of the test until you call Test.stopTest • System.RunAs : Change user contexts to either an existing user or a new user. All of that user's record sharing is then enforced • isTest(SeeAllData = true) : Use on class methods in exceptional cases where there are sObjects that doesn't allow DML operation e.g. PriceBook creation
  • 6. Example @isTest private class MyTestClass { @isTest static void test1(){ // Implement test code } }
  • 8. Follow Seema Dhingra Seema Dhingra Associate Manager Accenture Blog : https://salesforcecodelearn.blogsopt.in Twitter : @sfdc_Seema Facebook Page : @salesforcecodelearn