Achieve your business goals faster by unlocking our Salesforce Cloud Services.
Ex:
trigger PositionTrigger on Position__c (before update) {
if(Trigger.isBefore && Trigger.isUpdate) {
PositionTriggerHandler.handleBeforeUpdate(Trigger.newMap, Trigger.oldMap);
}
public class PositionTriggerHandler {
//static method called to handle the before update logic
public static void handleBeforeUpdate(Map<Id, Position__c> newMap, Map<Id, Position__c> oldMap){
//Loop over all the position records being updated
for(Position__c pos : newMap.Values()){
//Compare new Min Salary field value with old value
if(pos.Min_Salary__c <> oldMap.get(pos.Id).Min_Salary__c){
//If old and new values are not same, assign old value to comments field
pos.Comments__c = String.valueOf(oldMap.get(pos.Id).Min_Salary__c);
@isTest
public class PositionTriggerTest {
static testMethod void updateMinimumSalary(){
PositionTriggerTest.insertPositionRecord();
List<Position__c> positionListToUpdate = new List<Position__c>();
for(Position__c pos : [Select Min_Salary__c from Position__c]){
pos.Min_Salary__c = 30000;
positionListToUpdate.add(pos);
Test.startTest();
update positionListToUpdate;
Test.stopTest();
List<Position__c> posList = [Select Comments__c from Position__c];
system.assertEquals(posList[0].Comments__c, ‘20000’);
static void insertPositionRecord(){
List<Position__c> positionListToInsert = new List<Position__c>();
for(Integer i=0; i< 100; i++){
Position__c pos = new Position__c();
pos.Name = ‘Salesforce Developer’+i;
pos.Min_Salary__c = 20000;
positionListToInsert.add(pos);
insert positionListToInsert;
By default, Salesforce org’s data is not visible to the test class, rather it has to create its own data. However, this behavior can be changed by annotating a Test class or test method with @isTest(SeeAllData=true) annotation opens up the entire org’s data to the test class.
Characteristics of the IsTest(SeeAllData=true) Annotation are:
public class PriceBookTest {
// Utility method that can be called by Apex tests to create price book entries.
static testmethod void addPricebookEntries() {
// First, set up test price book entries.
// Insert a test product.
Product2 prod = new Product2(Name = ‘Laptop X200’,
Family = ‘Hardware’);
insert prod;
// Get standard price book ID.
// This is available irrespective of the state of SeeAllData.
Id pricebookId = Test.getStandardPricebookId();
// 1. Insert a price book entry for the standard price book.
// Standard price book entries require the standard price book ID we got earlier.
PricebookEntry standardPrice = new PricebookEntry(
Pricebook2Id = pricebookId, Product2Id = prod.Id,
UnitPrice = 10000, IsActive = true);
insert standardPrice;
To load data in a test class without writing many codes, follow these three steps:
Syntax:
List<sObject> ls = Test.loadData(Position__c.sObjectType, ‘positionResource’);
Loading Test Data—Example:
Here is an example for Position object data load via Test.loadData method. Static Resource name is “positionResource” and Test Class is “PositionTriggerTest”.
// Load the test accounts from the static resource
List<sObject> positionLoadRecordList = Test.loadData(Position__c.sObjectType, ‘positionResource’);
// Verify that all 9 test position records were created
system.assertEquals(positionLoadRecordList.size(), 9);
These are Special methods which are used to create test data that becomes accessible to the whole Test Class.
These methods use @TestSetup annotation.
private class CommonTestSetup {
@testSetup static void setup() {
// Create common test accounts
List<Account> testAccts = new List<Account>();
for(Integer i=0;i<2;i++) {
testAccts.add(new Account(Name = ‘TestAcct’+i));
insert testAccts;
@isTest static void testMethod1() {
// Get the first test account by using a SOQL query
Account acct = [SELECT Id FROM Account WHERE Name=’TestAcct0′ LIMIT 1];
// Modify first account
acct.Phone = ‘555-1212’;
// This update is local to this test method only.
update acct;
// Delete second account
Account acct2 = [SELECT Id FROM Account WHERE Name=’TestAcct1′ LIMIT 1];
// This deletion is local to this test method only.
delete acct2;
< < Previous
Next > >
The batch is going to start shortly. Fill the form given below to Register yourself now.
Fill the form below to get a Result
Fill the form below to get a demo of this course.
Online Salesforce Development Course is soon going to be launched. Please fill the form and we will notify you about the course.