19
DecChristmas Special : Upto 40% OFF! + 2 free courses - SCHEDULE CALL
The Apex testing framework enables you to write and execute tests for your Apex classes and triggers on the Force.com platform. Apex unit tests ensure high quality for your Apex code and let you meet requirements for deploying Apex.
The testing framework tests your Apex code. Test class code is written in a sandbox environment and deployed to a production org. Apex unit tests are also requirements for deploying and distributing Apex. The following are the benefits of Apex unit tests. Apex test class makes sure the apex code works as expected
We write Test Classes in Apex Salesforce for Unit Testing. We get to find the bugs in our code and fix it to give better output. Testing gives the Code Coverage, first of all, we should know What Code Coverage is? Code coverage which shows to which percentage the code works. The minimum code coverage is 75% in order to be deployed to Production from the sandbox.
Salesforce Training For Administrators & Developers
Creating Test Class
Read: Know The Best Tips To Be Followed For Salesforce Admin Certification Test
testMethod keyword:
Test.startTest() and Test.stopTest(): These are the standard test methods which are available for test classes. These methods contain the event or action for which we would be simulating our test.
Like in this example, we would be testing our trigger and helper class to simulate the firing trigger by updating the records as we have done to start and stop block.
@isTest annotation:
By using this annotation, you declared that this is a test class and it will not be counted against the organization's total code limit.
System.assert():
Read: Execution Rules of Salesforce Triggers
This method checks the expected output with the actual value. Trigger Example
trigger parent2child on Account (after update) {
list<id>ids = new list<id>();
list<contact>conlist = new list<contact>();
for(account a:trigger.new){
ids.add(a.id);
list<contact>con =[select id,phone,account.phone from contact where accountidin:ids];
for(contact c:con){
c.Phone=c.account.phone;
conlist.add(c);
}
updateconlist;
}
}
Test class @isTest
private class testForconPhone {
statictestMethod void testcheck()
{
account a = new account();
a.Name='testing';
a.Phone='2222';
insert a;
accountaaa=[select name ,phone from account where id=:a.id];
contact c = new contact();
c.lastname=a.Name;
c.AccountId=a.id;
c.Phone='2222';
contact cc = new contact();
cc.lastname=a.Name;
cc.AccountId=a.id;
cc.Phone='111';
list<contact> c1 = new list<contact>();
c1.add(c);
c1.add(cc);
insert c1;
try{
updateaaa;
}
catch (exception e){
}
system.assertEquals('2222','2222');
}
}
Run test Class Click on run test Here we find the code coverage percentage Batch class example
Global class batchexam2 implements Database.Batchable<sobject> { globalDatabase.QueryLocator start(Database.BatchableContextbc){ string query='select id,lastName,Department from contact'; returnDatabase.getQueryLocator(query); } global void execute (Database.BatchableContextbc ,list<contact>cons){ //list<contact> for(contact c:cons){ if(c.Department==null){ c.Department='Education'; } } update cons; } Global void finish(Database.BatchableContextbc){ batchexam ab= new batchexam(); database.executeBatch(ab); } }
Test Class @isTest private class TestForbatchexam2 { @isTest
private static void testbatchapex(){
list<contact>con = new list<contact>();
contact c = new contact();
c.LastName='Daniel';
c.FirstName='Jack';
c.Department=null;
con.add(c);
insert con;
test.startTest();
batchexam2 b = new batchexam2();
idjobid=Database.executeBatch(b,2);
test.stopTest();
list<contact> c1=[select lastname,Department from contact where id=:c.Id];
for(contact c2:c1){
if(c2.Department==null){
contact c3 = new contact();
c3.LastName='philips';
c3.Department='Education';
c1.add(c3);
}
}
update c1;
}
}
Copy and paste the code and run test class Class example
Read: What are Escalation Rules in Salesforce?
public class insertExample {
public Account acc {set;get;}
public string name {set;get;}
public string industry {set;get;}
public string phone {set;get;}
publicDMlex(){
acc=new Account();
}
publicPageReference create(){
account a = new account ();
a.name=name;
a.industry=industry;
a.Phone=phone;
insert a;
PageReference p = new PageReference('/'+a.Id);
return p;
}
}
Test class @isTest
@isTest
Private class DMLExampletest {
@isTest
static void testme(){
Account acc=new Account();
acc.name='TCS';
acc.Industry='Banking';
Insert acc;
DMLExample De=new DMLExample();
Account abc = new Account(Name='TCS');
De.acc = abc;
De.create();
Account abc1 = new Account(Name='TCS2');
De.acc = abc1;
De.create();
}
}
Learn Salesforce in the Easiest Way
Testing is an important part of Apex in Salesforce. Test classes are the code snippets that are developed to perform unit testing in Apex. You can utilize the above-mentioned practices to create a test class in Salesforce using Apex. If you have any other ideas, then don’t forget to mention that in the comments section below. Happy testing!
A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.
Cyber Security
QA
Salesforce
Business Analyst
MS SQL Server
Data Science
DevOps
Hadoop
Python
Artificial Intelligence
Machine Learning
Tableau
Search Posts
Related Posts
How to Improve Coding Skills & Become Better Salesforce Developer? 701.3k
Salesforce Tutorial - Ultimate Learning Guide for Beginners & Beyond 563.6k
A Guide on How to Get & Reset Security Token in Salesforce Lightning 133.5k
Salesforce Developer Salary Guide: How Much Can You Earn? 9k
How to Update, Insert, Export or Delete Data Using Apex Data Loader? 336k
Receive Latest Materials and Offers on Salesforce Course
Interviews