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
AAlex11AAlex11 

Testing Trigger Handler Class

Hi everyone,
I write a trigger on opportunity (before insert, after insert, before update, after update) now I  need to test it.
I put trigger logic in a trigger Handler Class.
My trigger has to populate a date field ContractDate , with today's date, on the object Procedure__c releted to Opportunity with a lookup relationship where the record type of Procedure__C has a certain value  and when School__c field ( on opportunity object) is populated.
.that's the handler class
public class OpportunityTriggerHandler {

public static void date_program() {

list<Opportunity> OppList = new list<Opportunity>();

RecordType rtype= [SELECT id, Name FROM RecordType WHERE sObjectType= 'Procedure__c' and name='Client'  limit 1];

List<Procedure__c> listtoupdate= [SELECT ContractDate__c FROM Procedure__c WHERE recordTypeId= :rtype.id] ;

for (Opportunity op: (List<Opportunity>)Trigger.new) {
    for (Procedure__c proc : listtoupdate){
if(op.School__c !=NULL) {


proc.Contractdate__c= date.today();


}
        else{
            proc.ContractDate__c= null;
            }
          
            

update listtoupdate;
  
}}

}
}

That's the test class
@isTest
private class AddOppTriggerTest
{
    static testMethod void testOpportunityTriggerHandler() {

    
    
    
    Opportunity o= new Opportunity(name='tester', Numero_di_Rate__c= '2', CloseDate=date.ValueOf('2016-09-21'), StageName='QUESTIONARIO');
    insert o;
  RecordType rtid= [SELECT id, Name FROM RecordType WHERE sObjectType= 'Procedure__c' and name='Client'  limit 1];
    Opportunity opp=[SELECT id from Opportunity limit 1];
 

    Procedure__c pr = new Procedure__c (Opportunita__c=opp.id, recordtypeid=rtid.id );  
     system.debug(pr.ContractDate__c);
    insert pr;
       
    if (o.School--c!=NULL  )
    {
        pr.Contractdate__c= date.today() ;  
    }
        o=[Select  school__c from Opportunity];
    pr = [Select ContractDate__c from Procedure__c];

        
    system.debug(o.School__c);   
    system.assertEquals(false,o.School__c !=null); 
        
    system.debug(pr.Contractdate__c==date.today()  );
    system.assertEquals(false, pr.ContractDate__c == date.today());
               
               
              
}
}
Trigger works fine and also the test passes but I can't achieve the code coverage necessary.
The problem is There's no coverage for the if statement.
Any suggestion? where am I wrong?
 
Anupama SamantroyAnupama Samantroy
Hi ,

In your test class when you are creating the Opportunity o, you also need to populate the School__c field.

Thanks
Anupama
AAlex11AAlex11
Thanks Anupama,
I did what you suggest, but the problem is still there.
I've  tried to populate school__c field but there's no coverage for if statement.
Other ideas?
Thanks a lot