+ Start a Discussion
Stella mariaStella maria 

Test class help for Trigger

Need Test class for this trigger

public with sharing class Rfleet_UpdateVehicleGroupStatus_TRG {

   public static void onAfterUpdate(list<VEH_Vehicle__c>lsvehicle) {
    
        list<VEH_Vehicle__c> lsvehObj= new list<VEH_Vehicle__c>();
        list<string> lsVehstatus= new list<string>();
        set<string> setvgroupid =new set<string>();
        List<string> lsgroupName = new List<string>();
        string strstatus=null;
              
        for(VEH_Vehicle__c Vehobj:lsvehicle){
                    
            if(Vehobj.ArisVhcStatus__c==Label.VEH_Erronous || Vehobj.ArisVhcStatus__c==Label.VEH_Registered){
                setvgroupid.add(Vehobj.VehicleGroup__c);

            }

        }
        lsgroupName.addAll(setvgroupid);
        
            for(integer j=0;j<lsgroupName.size();j++){
                Integer ko=0;
                Integer sent=0;
                Integer ok=0; 
                lsVehstatus.clear();
                strstatus=null;
                string strgroupid=null;
                lsvehObj=[Select id,ArisVhcStatus__c,VehicleGroup__c from VEH_Vehicle__c where VehicleGroup__c=:lsgroupName[j]];
                system.debug('group lsveh-->'+lsvehObj);  
               
               for(VEH_Vehicle__c lsStatus:lsvehObj){
                    lsVehstatus.add(lsStatus.ArisVhcStatus__c);
                                
                }
          
                    for(Integer i=0;i<lsVehstatus.size();i++){
                                           
                            if(lsVehstatus[i]==Label.VEH_Sent){
                                sent++;
                                 
                            }else if(lsVehstatus[i]==Label.VEH_Registered){
                                ok++;
                               
                            }else if(lsVehstatus[i]==Label.VEH_Erronous){
                                ko++;
                               
                            }
                        
                    }
            
                if(sent > 0){
                    if(ko > 0){
                        strstatus=Label.VEH_ReturnPartialKO;
                        
                    }else{
                        strstatus=Label.VEH_ReturnPartialOK;
                       
                        
                    }
                }else{
                    if(ko==0){
                        strstatus=Label.VEH_Validated;
                     
                    }else{
                        strstatus=Label.VEH_KO;
                     
                    }
                    
                }
             if(lsgroupName[j] !=null )
            {
            VHG_VehicleGroup__c updateVgroup=new VHG_VehicleGroup__c(id=lsgroupName[j],VHGStatus__c=strstatus);
            system.debug('final update group--->'+updateVgroup);
            update updateVgroup;
            }
        }
    }
      
}

Thanks in Advance
Best Answer chosen by Stella maria
karthikeyan perumalkarthikeyan perumal
Hello, 

Use Below TestClass
 
@isTest
public class Rfleet_Vehicle_update_TEST{
 
      
     public Static testMethod void Vehicleupdate_Registered(){
     
       Test.StartTest(); 
       VHG_VehicleGroup__c TestVHG= new VHG_VehicleGroup__c ();
       TestVHG.VHGStatus__c='Created';
        
       insert TestVHG;
                
       VEH_Vehicle__c  TestV= new VEH_Vehicle__c();
       TestV.VehicleGroup__c=TestVHG.id;
       TestV.ArisVhcStatus__c='Registered';
       Insert TestV;
       
       VEH_Vehicle__c  TestV1= new VEH_Vehicle__c();
       TestV1.VehicleGroup__c=TestVHG.id;
       TestV1.ArisVhcStatus__c='Sent';
       Insert TestV1;
       
       VEH_Vehicle__c  TestV2= new VEH_Vehicle__c();
       TestV2.VehicleGroup__c=TestVHG.id;
       TestV2.ArisVhcStatus__c='Erronous';
       Insert TestV2;    
              
       TestV.ArisVhcStatus__c='Registered';
       TestV1.ArisVhcStatus__c='Sent';
       TestV2.ArisVhcStatus__c='Erronous';
       update  TestV;
       update  TestV1;
       update  TestV2;
       update  TestVHG;
        
       Test.StopTest();
      }
     
     public Static testMethod void Vehicleupdate_Erronous(){
     
       Test.StartTest(); 
       VHG_VehicleGroup__c TestVHG= new VHG_VehicleGroup__c ();
       TestVHG.VHGStatus__c='Created';
        
       insert TestVHG;
                
       VEH_Vehicle__c  TestV= new VEH_Vehicle__c();
       TestV.VehicleGroup__c=TestVHG.id;
       TestV.ArisVhcStatus__c='Registered';
       Insert TestV;
       
       VEH_Vehicle__c  TestV1= new VEH_Vehicle__c();
       TestV1.VehicleGroup__c=TestVHG.id;
       TestV1.ArisVhcStatus__c='Sent';
       Insert TestV1;
       
       VEH_Vehicle__c  TestV2= new VEH_Vehicle__c();
       TestV2.VehicleGroup__c=TestVHG.id;
        TestV2.ArisVhcStatus__c='Erronous';
       Insert TestV2;    
              
       TestV.ArisVhcStatus__c='Registered';
       TestV1.ArisVhcStatus__c='Sent';
       TestV2.ArisVhcStatus__c='Registered';
       
       update  TestV2;
       update  TestV;
       update  TestV1;
                    
       update  TestVHG;     
       
       Test.StopTest();
      }
      
       public Static testMethod void Vehicleupdate_validated(){
     
       Test.StartTest(); 
       VHG_VehicleGroup__c TestVHG= new VHG_VehicleGroup__c ();
       TestVHG.VHGStatus__c='Created';
        
       insert TestVHG;
                
       VEH_Vehicle__c  TestV= new VEH_Vehicle__c();
       TestV.VehicleGroup__c=TestVHG.id;
       TestV.ArisVhcStatus__c='Registered';
       Insert TestV;
       
       VEH_Vehicle__c  TestV1= new VEH_Vehicle__c();
       TestV1.VehicleGroup__c=TestVHG.id;
       TestV1.ArisVhcStatus__c='Registered';
       Insert TestV1;
       
       VEH_Vehicle__c  TestV2= new VEH_Vehicle__c();
       TestV2.VehicleGroup__c=TestVHG.id;
        TestV2.ArisVhcStatus__c='Registered';
       Insert TestV2;    
              
       TestV.ArisVhcStatus__c='Registered';
       TestV1.ArisVhcStatus__c='Registered';
       TestV2.ArisVhcStatus__c='Registered';
       
       update  TestV2;
       update  TestV;
       update  TestV1;
                    
       update  TestVHG;     
       
       Test.StopTest();
      }
     
      public Static testMethod void Vehicleupdate_Ko(){
     
       Test.StartTest(); 
       VHG_VehicleGroup__c TestVHG= new VHG_VehicleGroup__c ();
       TestVHG.VHGStatus__c='Created';
        
       insert TestVHG;
                
       VEH_Vehicle__c  TestV= new VEH_Vehicle__c();
       TestV.VehicleGroup__c=TestVHG.id;
       TestV.ArisVhcStatus__c='Erronous';
       Insert TestV;
       
       VEH_Vehicle__c  TestV1= new VEH_Vehicle__c();
       TestV1.VehicleGroup__c=TestVHG.id;
       TestV1.ArisVhcStatus__c='Erronous';
       Insert TestV1;
       
       VEH_Vehicle__c  TestV2= new VEH_Vehicle__c();
       TestV2.VehicleGroup__c=TestVHG.id;
        TestV2.ArisVhcStatus__c='Erronous';
       Insert TestV2;    
              
       TestV.ArisVhcStatus__c='Erronous';
       TestV1.ArisVhcStatus__c='Erronous';
       TestV2.ArisVhcStatus__c='Erronous';
       
       
       update  TestV2;
       update  TestV;
       update  TestV1;
                    
       update  TestVHG;     
       
       Test.StopTest();
      }
      
      
}

Hope this will give you 100 % coverage. 
Mark Best ANSWER if its work for you. 

Thanks
karthik

 

All Answers

karthikeyan perumalkarthikeyan perumal
Hello, 

Use Below TestClass
 
@isTest
public class Rfleet_Vehicle_update_TEST{
 
      
     public Static testMethod void Vehicleupdate_Registered(){
     
       Test.StartTest(); 
       VHG_VehicleGroup__c TestVHG= new VHG_VehicleGroup__c ();
       TestVHG.VHGStatus__c='Created';
        
       insert TestVHG;
                
       VEH_Vehicle__c  TestV= new VEH_Vehicle__c();
       TestV.VehicleGroup__c=TestVHG.id;
       TestV.ArisVhcStatus__c='Registered';
       Insert TestV;
       
       VEH_Vehicle__c  TestV1= new VEH_Vehicle__c();
       TestV1.VehicleGroup__c=TestVHG.id;
       TestV1.ArisVhcStatus__c='Sent';
       Insert TestV1;
       
       VEH_Vehicle__c  TestV2= new VEH_Vehicle__c();
       TestV2.VehicleGroup__c=TestVHG.id;
       TestV2.ArisVhcStatus__c='Erronous';
       Insert TestV2;    
              
       TestV.ArisVhcStatus__c='Registered';
       TestV1.ArisVhcStatus__c='Sent';
       TestV2.ArisVhcStatus__c='Erronous';
       update  TestV;
       update  TestV1;
       update  TestV2;
       update  TestVHG;
        
       Test.StopTest();
      }
     
     public Static testMethod void Vehicleupdate_Erronous(){
     
       Test.StartTest(); 
       VHG_VehicleGroup__c TestVHG= new VHG_VehicleGroup__c ();
       TestVHG.VHGStatus__c='Created';
        
       insert TestVHG;
                
       VEH_Vehicle__c  TestV= new VEH_Vehicle__c();
       TestV.VehicleGroup__c=TestVHG.id;
       TestV.ArisVhcStatus__c='Registered';
       Insert TestV;
       
       VEH_Vehicle__c  TestV1= new VEH_Vehicle__c();
       TestV1.VehicleGroup__c=TestVHG.id;
       TestV1.ArisVhcStatus__c='Sent';
       Insert TestV1;
       
       VEH_Vehicle__c  TestV2= new VEH_Vehicle__c();
       TestV2.VehicleGroup__c=TestVHG.id;
        TestV2.ArisVhcStatus__c='Erronous';
       Insert TestV2;    
              
       TestV.ArisVhcStatus__c='Registered';
       TestV1.ArisVhcStatus__c='Sent';
       TestV2.ArisVhcStatus__c='Registered';
       
       update  TestV2;
       update  TestV;
       update  TestV1;
                    
       update  TestVHG;     
       
       Test.StopTest();
      }
      
       public Static testMethod void Vehicleupdate_validated(){
     
       Test.StartTest(); 
       VHG_VehicleGroup__c TestVHG= new VHG_VehicleGroup__c ();
       TestVHG.VHGStatus__c='Created';
        
       insert TestVHG;
                
       VEH_Vehicle__c  TestV= new VEH_Vehicle__c();
       TestV.VehicleGroup__c=TestVHG.id;
       TestV.ArisVhcStatus__c='Registered';
       Insert TestV;
       
       VEH_Vehicle__c  TestV1= new VEH_Vehicle__c();
       TestV1.VehicleGroup__c=TestVHG.id;
       TestV1.ArisVhcStatus__c='Registered';
       Insert TestV1;
       
       VEH_Vehicle__c  TestV2= new VEH_Vehicle__c();
       TestV2.VehicleGroup__c=TestVHG.id;
        TestV2.ArisVhcStatus__c='Registered';
       Insert TestV2;    
              
       TestV.ArisVhcStatus__c='Registered';
       TestV1.ArisVhcStatus__c='Registered';
       TestV2.ArisVhcStatus__c='Registered';
       
       update  TestV2;
       update  TestV;
       update  TestV1;
                    
       update  TestVHG;     
       
       Test.StopTest();
      }
     
      public Static testMethod void Vehicleupdate_Ko(){
     
       Test.StartTest(); 
       VHG_VehicleGroup__c TestVHG= new VHG_VehicleGroup__c ();
       TestVHG.VHGStatus__c='Created';
        
       insert TestVHG;
                
       VEH_Vehicle__c  TestV= new VEH_Vehicle__c();
       TestV.VehicleGroup__c=TestVHG.id;
       TestV.ArisVhcStatus__c='Erronous';
       Insert TestV;
       
       VEH_Vehicle__c  TestV1= new VEH_Vehicle__c();
       TestV1.VehicleGroup__c=TestVHG.id;
       TestV1.ArisVhcStatus__c='Erronous';
       Insert TestV1;
       
       VEH_Vehicle__c  TestV2= new VEH_Vehicle__c();
       TestV2.VehicleGroup__c=TestVHG.id;
        TestV2.ArisVhcStatus__c='Erronous';
       Insert TestV2;    
              
       TestV.ArisVhcStatus__c='Erronous';
       TestV1.ArisVhcStatus__c='Erronous';
       TestV2.ArisVhcStatus__c='Erronous';
       
       
       update  TestV2;
       update  TestV;
       update  TestV1;
                    
       update  TestVHG;     
       
       Test.StopTest();
      }
      
      
}

Hope this will give you 100 % coverage. 
Mark Best ANSWER if its work for you. 

Thanks
karthik

 
This was selected as the best answer
Stella mariaStella maria
Thanks for your help. its works