function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Varsha Pawar 4Varsha Pawar 4 

Help in writing Test class for an Apex Trigger

Hi Team,

Can someone please help me writing a Test class for the below trigger?

public class AccountTriggerHandler {
    public static void executeTriggerHandler(){
        //get Account new and old List
        List<Account> accountNewList = Trigger.New;
        List<Account> accountOldList = Trigger.Old;
       
        Map<Id,Account> mapNewAccount = (Map<Id,Account>)trigger.newMap;
        Map<Id,Account> mapOldAccount = (Map<Id,Account>)trigger.oldMap;
       
        List<Information__c> lstInfo = [SELECT Id, AccountID__c FROM Information__c];
        Map<id, id> mapAcct = new Map<id, id>();
        for(Information__c infoItem : lstInfo){
            if(!mapAcct.containsKey(infoItem.AccountID__c))
                mapAcct.put(infoItem.AccountID__c, infoItem.Id);
        }
        List<Information__c> infoList = new List<Information__c>();
        Information__c informationItem = null;
        //New Account is created so insert in Information
        if(accountOldList == NULL){
            for(Account a: accountNewList){
                informationItem = new Information__c();
                informationItem.Name = a.Name;
                informationItem.Street__c = a.BillingStreet;
                informationItem.City__c = a.BillingCity;
                informationItem.State__c = a.BillingState;
                informationItem.Postal_Code__c = a.BillingPostalCode;
                informationItem.Country__c= a.BillingCountry;
                informationItem.AccountID__c = a.Id;
                infoList.add(informationItem);
            }
        }
        if(Trigger.isAfter)
        {
            //Insert Information
            if(trigger.isInsert){
                insert infoList;
            }
            //Insert/Update/Delete Information
            if(trigger.isUpdate){
                List<Information__c> insertInformationList = new List<Information__c>();
                List<Information__c> updateInformationList = new List<Information__c>();
                List<Information__c> deleteInformationList = new List<Information__c>();
                for(Id accountId : mapNewAccount.keyset()){
                    informationItem = new Information__c();
                    informationItem.Name = mapNewAccount.get(accountId).Name;
                    informationItem.Street__c = mapNewAccount.get(accountId).BillingStreet;
                    informationItem.City__c = mapNewAccount.get(accountId).BillingCity;
                    informationItem.State__c = mapNewAccount.get(accountId).BillingState;
                    informationItem.Postal_Code__c = mapNewAccount.get(accountId).BillingPostalCode;
                    informationItem.Country__c= mapNewAccount.get(accountId).BillingCountry;
                    informationItem.AccountID__c = mapNewAccount.get(accountId).Id;
                    if(mapNewAccount.get(accountId).Information__c != mapOldAccount.get(accountId).Information__c){
                        if(mapNewAccount.get(accountId).Information__c)
                        {
                            //Insert Information
                            insertInformationList.add(informationItem);
                        }
                        else
                        {
                            //get info Id
                            informationItem.Id = mapAcct.get(accountId);
                            //Delete Information
                            deleteInformationList.add(informationItem);
                        }
                    }
                    else
                    {
                        //Update Information
                        informationItem.Id = mapAcct.get(accountId);
                        updateInformationList.add(informationItem);
                    }
                }
                //Insert Information
                if(insertInformationList.size() > 0){
                    insert insertInformationList;
                }
                //Update Information
                if(updateInformationList.size() > 0){
                    update updateInformationList;
                }
                //Delete Information
                if(deleteInformationList.size() > 0){
                    delete deleteInformationList;
                }
            }
        }
    }
}
Thanks in advance
ShirishaShirisha (Salesforce Developers) 
Hi Varsha,

Greetings!

Please find the sample test code to delete the record:

@isTest private class testClass { static testMethod void testDel() { Account testAcc = new Account (); testAcc.Name = 'TestAccount'; insert testAcc; Test.startTest(); Test.setCurrentPage(Page.MyVFPage); ApexPages.StandardController sc = new ApexPages.StandardController(testAcc); MyCtrlExtension TE = new MyCtrlExtension(sc); TE.FunctionToDeleteRecord(); Account deletedAccount = [SELECT Id, IsDeleted FROM Account WHERE Id = :testAcc.Id ALL ROWS]; System.asserEquals(deletedAccount.IsDeleted, true); Test.stopTest(); } }

Also,please try to add the logic for updating the record in the same way to cover the class.

Please check belo blog for test classes.
http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Warm Regards,
Shirisha Pathuri