+ Start a Discussion
VFVF 

code coverage test class for a trigger

Hello, 

 

Here is the trigger which i have written:

This trigger used to update the contact fields when any custom field is updated.

I wrote a test case for the below trigger for that: 

I am getting a code coverage of 6% . can any one help me to get the code coverage.

 

trigger WCSFTrigger on Individual_Email_Results__c (after update) {
Map<String, Individual_Email_Results__c> lgaMap = new Map<String, Individual_Email_Results__c>();
       for (Individual_Email_Results__c objUpdate: System.Trigger.New) {        
        Individual_Email_Results__c obj= [Select id,Contact__c,Lead__c,Hard_Bounce__c,Abuse__c,GlobalUnsubscribe__c,UniversalUnsubscribe__c,Unsubscribe__c,Hard_Bounce_Event_Date__c  from Individual_Email_Results__c where id= :objUpdate.Id];
        string contactID = obj.Contact__c;
        string leadID = obj.Lead__c;
        Decimal newValue = trigger.new[0].Hard_Bounce__c;
        Decimal oldValue =  trigger.old[0].Hard_Bounce__c;

        if(newValue != oldValue)
        {
            if(contactID != null)
            {  
               Contact contact = [Select Id, HasOptedOutOfEmail from contact WHERE Id=:contactID ];
               if(contact.HasOptedOutOfEmail == true)
               {
                  contact.HasOptedOutOfEmail = false;
               }
               else
               {
                  contact.HasOptedOutOfEmail = true;
               }
               update contact;
                   
            }
            if(leadId != null)
            {              
               Lead lead = [Select Id, HasOptedOutOfEmail from lead WHERE Id=: leadId];
               if(lead.HasOptedOutOfEmail == true)
               {
                  lead.HasOptedOutOfEmail = false;
               }
               else
               {
                   lead.HasOptedOutOfEmail = true;
               }
               update lead;
            }
             
        }        
        if( (trigger.new[0].Abuse__c != trigger.old[0].Abuse__c) || (trigger.new[0].GlobalUnsubscribe__c != trigger.old[0].GlobalUnsubscribe__c) || (trigger.new[0].UniversalUnsubscribe__c != trigger.old[0].UniversalUnsubscribe__c) || (trigger.new[0].Unsubscribe__c != trigger.old[0].Unsubscribe__c))
        {
            if(contactID != null)
            {  
               Contact contact = [Select Id, EmailBouncedDate , EmailBouncedReason from contact WHERE Id=:contactID ];
               contact.EmailBouncedReason = 'WhatCounts Hard Bounce';
               contact.EmailBouncedDate = datetime.valueOf(obj.Hard_Bounce_Event_Date__c);
               update contact;
                   
            }
            if(leadId != null)
            {              
               Lead lead= [Select Id, EmailBouncedDate , EmailBouncedReason from lead WHERE Id=:leadID ];
               lead.EmailBouncedReason = 'WhatCounts Hard Bounce';
               lead.EmailBouncedDate =  datetime.valueOf(obj.Hard_Bounce_Event_Date__c);
               update lead;
            }
         
        }
        }

 

 

Now here is the Test class for the trigger:

 

 

public class sampleTest{
static testMethod void WCSFTrigger(){
Map<String, Individual_Email_Results__c> lgaMap = new Map<String, Individual_Email_Results__c>();

Individual_Email_Results__c objInsert = new Individual_Email_Results__c();
//List<IndividualEmailResult__c> itemsToInsert = new List<IndividualEmailResult__c>(); 
objInsert .Hard_Bounce__c = 1; 
objInsert .Abuse__c = '1';
objInsert .GlobalUnsubscribe__c = '1';
objInsert .UniversalUnsubscribe__c = '1';
objInsert .Unsubscribe__c = '1';
objInsert .Lead__c = '00Q90000001S4fL';
objInsert .Contact__c = '00390000001bZ1V';


//itemsToInsert.add(objInsert );
insert objInsert ;
Individual_Email_Results__c obj= [Select id,Contact__c,Lead__c,Hard_Bounce__c,Abuse__c,GlobalUnsubscribe__c,UniversalUnsubscribe__c,Unsubscribe__c,Hard_Bounce_Event_Date__c  from Individual_Email_Results__c where id= :objInsert .Id];
Decimal newValue = obj.Hard_Bounce__c;
Decimal oldValue = 2; 
if(newValue != oldValue)
{
    if(obj.Contact__c!= null)
    {
        Contact contact = [Select Id, HasOptedOutOfEmail from contact WHERE Id=:obj.Contact__c];
         if(contact.HasOptedOutOfEmail == true)
               {
                 system.assertEquals(contact.HasOptedOutOfEmail , true); 
               }
               else
               {
                  system.assertEquals(contact.HasOptedOutOfEmail , true); 

               }
               update contact;
    }
    
    if(obj.Lead__c!= null)
    {
        Lead lead = [Select Id, HasOptedOutOfEmail from lead WHERE Id=:obj.Lead__c];
         if(lead .HasOptedOutOfEmail == true)
               {
                 system.assertEquals(lead .HasOptedOutOfEmail , true); 
               }
               else
               {
                  system.assertEquals(lead .HasOptedOutOfEmail , true); 

               }
               update lead ;
    }
    
     if( (obj.Abuse__c != '2') || (obj.GlobalUnsubscribe__c != '2') || (obj.UniversalUnsubscribe__c != '2') || (obj.Unsubscribe__c != '2'))
        {
            if(obj.Contact__c!= null)
            {  
               Contact contact = [Select Id, EmailBouncedDate , EmailBouncedReason from contact WHERE Id=:obj.Contact__c];
                system.assertEquals( contact.EmailBouncedReason , 'WhatCounts Hard Bounce'); 
                //system.assertEquals( contact.EmailBouncedDate , datetime.valueOf('2009-11-24 10:26:00') );
               update contact;
                   
            }
            if(obj.Lead__c!= null)
            {              
               Lead lead= [Select Id, EmailBouncedDate , EmailBouncedReason from lead WHERE Id=:obj.Lead__c];
               system.assertEquals( lead.EmailBouncedReason , null); 
                //system.assertEquals( lead.EmailBouncedDate , datetime.valueOf('2009-11-24 10:26:00') );

               update lead;
            }
         
        }
}       
 }

 

Thanks

shaan