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
Girbson Bijou 8Girbson Bijou 8 

Test Class Coverage Increasing

Please help me to increase the code coverage of this class. actually is is only at 25%. the class is to increment in field each time a visualforce page called "DeliveryPage" is launched.  This field is on an object called Delivery__c which is a child of Account
public class DeliveryPageClass {
   // public Delivery__c delivery{get;set;}
public id deliveryId;
    public DeliveryPageClass(ApexPages.StandardController stdController)
    {
        deliveryId=stdController.getId();
     
    }
    public void updateDelivery()
    {
        Delivery__c delivery = [select id,Print_Counter__c from Delivery__c where id=:deliveryId];
        if(delivery.Print_Counter__c==null)
        {
            delivery.Print_Counter__c=1;
        }
        else
        {
            delivery.Print_Counter__c=delivery.Print_Counter__c+1;
        }
        system.debug(delivery);
        update delivery;
        system.debug(delivery);
        
    }

}

Test Class
@isTest
public class DeliveryPageClassTest {
    static testMethod void TestDeliveryPageClass(){
     Account pa = new Account(Name ='test Account parent', Representant__c ='hkjlhjd', 
     Distribution_Center__c='CRD Cayes', Class__c = 'Clinic', Type = 'CLINICS', 
     Departement__c = 'NORD', Address__c ='Test Address');
     insert pa;
         
 Account a = new Account();
 a.Name ='test Account'; 
 a.Representant__c ='Bijou';
 a.Distribution_Center__c='FFP Caracole';
 a.Class__c = 'NGO'; 
 a.Type = 'CLINICS';
 a.Departement__c = 'NORD';
 a.Address__c ='Test Address';
 a.ParentId = pa.ID;
 insert a ;
    
        Delivery__c del = new Delivery__c();
        del.Beneficiaire__c = a.Id;
        del.Ration__c =3;
        del.Delivery_status__c='Pending';
        
        insert del;
          
       
        ApexPages.StandardController sc = new ApexPages.standardController(new Delivery__c());
        DeliveryPageClass dpc = new DeliveryPageClass(sc);
    }
}
 
 
Best Answer chosen by Girbson Bijou 8
Maharajan CMaharajan C
Hi Girbson,

Please try the below test class:

@isTest
public class DeliveryPageClassTest {

    static testMethod void TestDeliveryPageClass(){
     Account pa = new Account(Name ='test Account parent', Representant__c ='hkjlhjd', 
     Distribution_Center__c='CRD Cayes', Class__c = 'Clinic', Type = 'CLINICS', 
     Departement__c = 'NORD', Address__c ='Test Address');
     insert pa;
             
     Account a = new Account();
     a.Name ='test Account'; 
     a.Representant__c ='Bijou';
     a.Distribution_Center__c='FFP Caracole';
     a.Class__c = 'NGO'; 
     a.Type = 'CLINICS';
     a.Departement__c = 'NORD';
     a.Address__c ='Test Address';
     a.ParentId = pa.ID;
     insert a ;
    
        Delivery__c del = new Delivery__c();
        del.Beneficiaire__c = a.Id;
        del.Ration__c =3;
        del.Delivery_status__c='Pending';
        del.Print_Counter__c = 1;
        
        insert del;
       
        ApexPages.StandardController sc = new ApexPages.standardController(del);
        DeliveryPageClass dpc = new DeliveryPageClass(sc);
        dpc.updateDelivery();
    }
    
    static testMethod void TestDeliveryPageClass1(){

     Account pa = new Account(Name ='test Account parent', Representant__c ='hkjlhjd', 
     Distribution_Center__c='CRD Cayes', Class__c = 'Clinic', Type = 'CLINICS', 
     Departement__c = 'NORD', Address__c ='Test Address');
     insert pa;
         
     Account a = new Account();
     a.Name ='test Account'; 
     a.Representant__c ='Bijou';
     a.Distribution_Center__c='FFP Caracole';
     a.Class__c = 'NGO'; 
     a.Type = 'CLINICS';
     a.Departement__c = 'NORD';
     a.Address__c ='Test Address';
     a.ParentId = pa.ID;
     insert a ;
    
        Delivery__c del = new Delivery__c();
        del.Beneficiaire__c = a.Id;
        del.Ration__c =3;
        del.Delivery_status__c='Pending';
        del.Print_Counter__c = null;
        
        insert del;
       
        ApexPages.StandardController sc = new ApexPages.standardController(del);
        DeliveryPageClass dpc = new DeliveryPageClass(sc);
        dpc.updateDelivery();
    }
}


Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Girbson,

Please try the below test class:

@isTest
public class DeliveryPageClassTest {

    static testMethod void TestDeliveryPageClass(){
     Account pa = new Account(Name ='test Account parent', Representant__c ='hkjlhjd', 
     Distribution_Center__c='CRD Cayes', Class__c = 'Clinic', Type = 'CLINICS', 
     Departement__c = 'NORD', Address__c ='Test Address');
     insert pa;
             
     Account a = new Account();
     a.Name ='test Account'; 
     a.Representant__c ='Bijou';
     a.Distribution_Center__c='FFP Caracole';
     a.Class__c = 'NGO'; 
     a.Type = 'CLINICS';
     a.Departement__c = 'NORD';
     a.Address__c ='Test Address';
     a.ParentId = pa.ID;
     insert a ;
    
        Delivery__c del = new Delivery__c();
        del.Beneficiaire__c = a.Id;
        del.Ration__c =3;
        del.Delivery_status__c='Pending';
        del.Print_Counter__c = 1;
        
        insert del;
       
        ApexPages.StandardController sc = new ApexPages.standardController(del);
        DeliveryPageClass dpc = new DeliveryPageClass(sc);
        dpc.updateDelivery();
    }
    
    static testMethod void TestDeliveryPageClass1(){

     Account pa = new Account(Name ='test Account parent', Representant__c ='hkjlhjd', 
     Distribution_Center__c='CRD Cayes', Class__c = 'Clinic', Type = 'CLINICS', 
     Departement__c = 'NORD', Address__c ='Test Address');
     insert pa;
         
     Account a = new Account();
     a.Name ='test Account'; 
     a.Representant__c ='Bijou';
     a.Distribution_Center__c='FFP Caracole';
     a.Class__c = 'NGO'; 
     a.Type = 'CLINICS';
     a.Departement__c = 'NORD';
     a.Address__c ='Test Address';
     a.ParentId = pa.ID;
     insert a ;
    
        Delivery__c del = new Delivery__c();
        del.Beneficiaire__c = a.Id;
        del.Ration__c =3;
        del.Delivery_status__c='Pending';
        del.Print_Counter__c = null;
        
        insert del;
       
        ApexPages.StandardController sc = new ApexPages.standardController(del);
        DeliveryPageClass dpc = new DeliveryPageClass(sc);
        dpc.updateDelivery();
    }
}


Thanks,
Maharajan.C
This was selected as the best answer
Girbson Bijou 8Girbson Bijou 8
And i get 100% coverage. Thank you so much @Maharajan