+ Start a Discussion
Sangeetha TSangeetha T 

Test classes for the following trigger

Hi I'm stuck up in writing the test classes for the following triggers to achieve code coverage while deployment 

What would be the test class for the following trigger 

trigger 1

trigger checktheLeadCheckBox on Contact (after update) 
{
  for(Contact c : Trigger.New)
  {
    List<Lead> li = [select Id, Customer_of_PubKCyber__c,Email,Customer_of_PubKLaw__c from Lead where Email =: c.Email AND IsConverted =: false]; 
    for(Lead l : li)
    {
     If(l.Id != null)
     {
      
      
       l.Customer_of_PubKCyber__c=c.Customer_of_PubKCyber__c;
       l.Customer_of_PubKLaw__c = c.Customer_of_PubKLaw__c;
       update l;
     }
    }
  }
}

Trigger 2

trigger updatecontactcheckbox on Opportunity (after insert, after update) 
{    
    for(Opportunity o : Trigger.New)
    {
  
     if( o.Contact__c != null)                 
     {      
       Contact c = [select Id, Customer_of_PubKCyber__c,Email,Customer_of_PubKLaw__c from Contact where Id =: o.Contact__c ]; 
       
if((o.Subscription_Start_Date__c <= System.Today()) && (o.Subscription_End_Date__c >= System.Today()) && (o.Product_Name__c == 'PubKCyber Newsletter'))
    {
         c.Customer_of_PubKCyber__c = True;
        
    }
    else if(o.Product_Name__c == 'PubKCyber Newsletter')
         { 
           c .Customer_of_PubKCyber__c = false;
           
         }
          
if((o.Subscription_Start_Date__c <= System.Today()) && (o.Subscription_End_Date__c >= System.Today()) && (o.Product_Name__c == 'PubKLaw Newsletter'))
        { 
          c.Customer_of_PubKLaw__c= True;
         
        }
       else if(o.Product_Name__c == 'PubKLaw Newsletter')
         { 
           c.Customer_of_PubKLaw__c = false;            
         }
       update c; 
      }   
     }  
  }
Agustina GarciaAgustina Garcia
Hi,

For the first trigger I would do something like this:
 
Contact myc = new Contact();
//TODO populate required fields
myc.Email = 'myemail@gmail.com';
myc.Customer_of_PubKCyber__c = 'a';
myc.Customer_of_PubKLaw__c = 'b';
//add more contact field values
insert myc;

List<Lead> leadsToInsert = new List<Lead>();
for(Integer i=0; i<2; i++)
{
   Lead myLead = new Lead();
   //TODO populate required fields
   myLead.Email = 'myemail@gmail.com';
   //add more fields values

   leadsToInsert.add(myLead);

}
insert leadsToInsert;

myc.Customer_of_PubKLaw__c = 'c';
update myc;

This should execue the after update trigger

For the second one I would do something similar. Create Opportunities and after insert and update they should call the trigger. Depending on your records values, it would go inside your code or not.

Hope this helps as start poing
Sangeetha TSangeetha T
Thank you Agustina, Please help me with the second one also ...
im extremely new test class 

Thanks in advance, it would be great if yu help me out 
Agustina GarciaAgustina Garcia
Please, take into account that I didn't try. I only know that it compiles and the theory says that it should work. 

To cover second trigger:
 
    static testMethod void testOpportunity1()
    {
        Contact myc = new Contact();
        myc.Email = 'myemail@gmail.com';
        myc.Customer_of_PubKCyber__c = 'a';
        myc.Customer_of_PubKLaw__c = 'b';
        //add more contact field values
        insert myc;

        //This would cover first if
        Opportunity opp1 = new Opportunity();
        //TODO populate all requiredFields
        opp1.Contact__c = myc.Id;
        opp1.Subscription_Start_Date__c = System.today().addDays(-1);
        opp1.Subscription_End_Date__c = System.today().addDays(3);
        opp1.Product_Name__c = 'PubKCyber Newsletter';
        insert opp1;
        
        //This would cover else if line
		Opportunity opp2 = new Opportunity();
        //TODO populate all requiredFields
        opp2.Contact__c = myc.Id;
        opp2.Subscription_Start_Date__c = System.today().addDays(10);
        opp2.Subscription_End_Date__c = System.today().addDays(3);
        opp2.Product_Name__c = 'PubKCyber Newsletter';
        insert opp2;
        
		//This would cover second if
		Opportunity opp3 = new Opportunity();
        //TODO populate all requiredFields
        opp3.Contact__c = myc.Id;
        opp3.Subscription_Start_Date__c = System.today().addDays(-1);
        opp3.Subscription_End_Date__c = System.today().addDays(3);
        opp3.Product_Name__c = 'PubKLaw Newsletter';
        insert opp3;
        
		//This would cover second else if
		Opportunity opp4 = new Opportunity();
        //TODO populate all requiredFields
        opp4.Contact__c = myc.Id;
        opp4.Subscription_Start_Date__c = System.today().addDays(10);
        opp4.Subscription_End_Date__c = System.today().addDays(3);
        opp4.Product_Name__c = 'PubKLaw Newsletter';
        insert opp4;
    }

Hope this helps.
 
praveen sonepraveen sone
@Augustina Garcia : Aren't you missing out assertions altogether?

Note: Writing a test class with as many assertions as possible is the robust way. watsay?
Agustina GarciaAgustina Garcia
Totally agree. I focused on help to explain how to cover a trigger (they are invoqued via DML) rather than asserting things. But it is true that a test without asserts, even it can achieve 100%, they are not valid for Salesforce in case you need to pass their Security Review, for instance.

In fact, a test is to check that your code is working as expected.

So Sangeetha, for instance, for second trigger test, after each opportunity insertion you can check if the contact value is the expected one.

Something like this
 
Id contactId = myc.Id;
myc = [Select Customer_of_PubKCyber__c From Contact where Id = :contactId];
System.assertEquals(True, myc.Customer_of_PubKCyber__c);