+ Start a Discussion
Milan VadhiaMilan Vadhia 

How to write test class for an apex trigger?


As I am new in salesforce, please someone help me to write a test class for this given triggers..... Thank you.
ContactTrigger.apxt
trigger ContactTrigger on Contact (after insert, after update, after delete, after undelete) {

// Trigger to Insert contact Name from Contact CSV field of Account    
     if(Trigger.isInsert){
         if(Trigger.isAfter){
             ContactHandler insertContact = new ContactHandler();
             insertContact.onAfterInsert(Trigger.new);
        }
     }

// Trigger to Update contact Name from Contact CSV field of Account    
     if(Trigger.isUpdate){
         if(Trigger.isAfter){
             ContactHandler updateContact = new ContactHandler();
             updateContact.onAfterUpdate(Trigger.new , Trigger.oldMap);
        }
     }
     
// Trigger to Delete contact Name from Contact CSV field of Account 
     if(Trigger.isDelete){
         if(Trigger.isAfter){
             ContactHandler deleteContact = new ContactHandler();
             deleteContact.onAfterDelete(Trigger.old);
        }
     }
    
// Trigger to Undelete contact Name from Contact CSV field of Account    
     if(Trigger.isUndelete){
         if(Trigger.isAfter){
             ContactHandler undeleteContact = new ContactHandler();
             undeleteContact.onAfterUndelete(Trigger.new);
        }
     }
}   // End of Trigger ContactTrigger

ContactHandler.apxc
-----------------------------
public class ContactHandler {
    
    // Insert Trigger Handler Class
    public void onAfterInsert(List<Contact> lstContact){
        updateContactNameOnAccount(lstContact, new Map<Id, Contact>());
    }      // End of onAfterInsert Class

    // Update Trigger Handler Class
    public void onAfterUpdate(List<Contact> lstContact , Map<Id, Contact> mapContactOld){
        updateContactNameOnAccount(lstContact, mapContactOld);
    }      // End of onAfterUpdate Class

    // Delete Trigger Handler Class
    public void onAfterDelete(List<Contact> lstContact){
        updateContactNameOnAccount(lstContact, new Map<Id, Contact>());
    }      // End of onAfterDelete Class

    // Delete Trigger Handler Class
    public void onAfterUndelete (List<Contact> lstContact){
        updateContactNameOnAccount(lstContact, new Map<Id, Contact>());
    }   // End of onAfterUndelete Class
    
    private void updateContactNameOnAccount(List<Contact> lstContactNew, Map<Id, Contact> mapContactOld){
        Set<Id> setAccountId = new Set<Id>();
        for(Contact objContact : lstContactNew){
            if(mapContactOld.containskey(objContact.Id)) setAccountId.add(mapContactOld.get(objContact.Id).AccountId);
            setAccountId.add(objContact.AccountId);
        }
        List<Account> lstAccount = new List<Account>();
        for(Account objAccount : [SELECT Id, (SELECT Id, Name FROM Contacts) FROM Account WHERE Id IN: setAccountId]){
            List<String> lstContactName = new List<String>();
            for(Contact objContact : objAccount.Contacts){ 
                lstContactName.add(objContact.Name);
            }
            objAccount.Contact_CSV__c = String.join(lstContactName, ', ');
            lstAccount.add(objAccount);
        }
        update lstAccount;
    }
}   // End of ContactTriggerHandler Class


 
Best Answer chosen by Milan Vadhia
Milan VadhiaMilan Vadhia
Here is the TEST CLASS CODE for above Trigger and Handler class which will give 100% Coverage...

ContactTrigger.apxt :     100% Coverage
ContactHandler.apxc :   100% Coverage

 
@isTest
private class ContactTest {
    
    Private static testMethod void contactDmlTest() {
        
        Test.startTest();
        
        // To Insert Contact
        Account testAccount = new Account();
        testAccount.Name = 'TechM Creata';
        insert testAccount;
        
        Contact oneContact = new Contact();
        oneContact.LastName = 'Gautam';
        oneContact.Phone = '101010';
        oneContact.AccountId = testAccount.Id;
        insert oneContact;
        List<Contact> ttContact = new List<Contact>();
        ttContact.add(oneContact);
        List<String> lstContactName = new List<String>();
        for(Contact objContact : ttContact){
            lstContactName.add(objContact.Name);
        }
            testAccount.Contact_CSV__c = String.join(lstContactName, ', ');
        
        
        // To Update Contact
        List<Contact>lstContactUpdate = new List<Contact>([SELECT Id, LastName, Phone FROM Contact WHERE Id =:oneContact.Id]); 
        lstContactUpdate[0].LastName='Reshma';
        lstContactUpdate[0].Phone='123123';
        update lstContactUpdate;
        
        // To Delete Contact
        oneContact = [SELECT Id, LastName, Phone FROM Contact WHERE Id =:oneContact.Id];
        Delete oneContact;
    
        // To Undelete Contact
        List<Contact>lstContactUndelete = new List<Contact>([SELECT Id, LastName, Phone FROM Contact WHERE Id =:oneContact.Id ALL ROWS]);  
        UnDelete lstContactUndelete;
        
        Test.stopTest();
    }
}

 

All Answers

Milan VadhiaMilan Vadhia
Here is the TEST CLASS CODE for above Trigger and Handler class which will give 100% Coverage...

ContactTrigger.apxt :     100% Coverage
ContactHandler.apxc :   100% Coverage

 
@isTest
private class ContactTest {
    
    Private static testMethod void contactDmlTest() {
        
        Test.startTest();
        
        // To Insert Contact
        Account testAccount = new Account();
        testAccount.Name = 'TechM Creata';
        insert testAccount;
        
        Contact oneContact = new Contact();
        oneContact.LastName = 'Gautam';
        oneContact.Phone = '101010';
        oneContact.AccountId = testAccount.Id;
        insert oneContact;
        List<Contact> ttContact = new List<Contact>();
        ttContact.add(oneContact);
        List<String> lstContactName = new List<String>();
        for(Contact objContact : ttContact){
            lstContactName.add(objContact.Name);
        }
            testAccount.Contact_CSV__c = String.join(lstContactName, ', ');
        
        
        // To Update Contact
        List<Contact>lstContactUpdate = new List<Contact>([SELECT Id, LastName, Phone FROM Contact WHERE Id =:oneContact.Id]); 
        lstContactUpdate[0].LastName='Reshma';
        lstContactUpdate[0].Phone='123123';
        update lstContactUpdate;
        
        // To Delete Contact
        oneContact = [SELECT Id, LastName, Phone FROM Contact WHERE Id =:oneContact.Id];
        Delete oneContact;
    
        // To Undelete Contact
        List<Contact>lstContactUndelete = new List<Contact>([SELECT Id, LastName, Phone FROM Contact WHERE Id =:oneContact.Id ALL ROWS]);  
        UnDelete lstContactUndelete;
        
        Test.stopTest();
    }
}

 
This was selected as the best answer
RAMYA M 60RAMYA M 60
i need test classes for this trigger

trigger Deleterecordondosage on NNIO_Dosage__c (before delete) {
for (NNIO_Dosage__c dos:trigger.old ){
        if (dos.NNIO_Care_Plan__c != Null )
        {
            string  cid =dos.NNIO_Care_Plan__c;
            case c =[select status from case where id =:cid];
            if(c.status=='Closed')
        {
       dos.addError('can not delete the dosage');
}  
  else if(c.NNIO_OnboardedPatient__c == True) 
  {
   dos.addError('can not delete the dosage') ;  
  }
        }
   }


}