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
steve456steve456 

Very Urgent----Test Class for Trigger

When i write a  test class for a trigger the "if condition part of code is getting covered but not the else how to cover the else part."

 

Part of Trigger is 

 for(Patient__c p:Trigger.new)
         {
           if(p.Associate__c!=null) 
           {
               p.Address_1__c = assMap.get(p.Associate__c).Current_Address__c; 
               p.City__c = assMap.get(p.Associate__c).Current_City__c; 
               p.State__c=assMap.get(p.Associate__c).Current_State__c;
               p.Zip__c=assMap.get(p.Associate__c).Current_Zip__c;
               
           }
           else
           {
             p.Address_1__c = ''; 
             p.City__c = ''; 
             p.State__c='';
             p.Zip__c='';
            
           }     //This part is not getting covered with my test class ...please help me //


Test class

 Associate__c aa =new Associate__c(Name='TestAss' ,Associate_ID__c='ASSID', Last_Name__c='AssLName',Current_Address__c='TestAddress',Current_City__c='TestCity',Current_Zip__c='45678',Current_State__c='TX');          
  insert aa;  

 

Patient__c p=new Patient__c(Name='TestUser',Associate__c=aa.id,CID__c='ASSID',Type__c='Agent',Address_1__c='TestAddressStreet',City__c='TestCity',State__c='TX',Zip__c='75423');
         insert p;
         update p;

 

 

 

 

BerettaJonBerettaJon

try creating and inserting another Patient.  Make sure the second patitient object does not meet  the if condition.  Make a patitient whose Associciate__C field does = null.

steve456steve456

Patient__c p1=new Patient__c(Name='TestUser',Associ​ate__c=null,CID__c='HID',Type__c='Agent',Addres​s_1__c='',City__c='',Stat​e__c='',Zip__c='');
         insert p1;
         update p1;

 

 

I gave this wawy tested but it didnot go into the else loop