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
Neha KakrooNeha Kakroo 

Code coverage of Test class

Hi I am new to writing test classes. My code coverage is only 65%. Can somebody pls help.

 

The class is:

public class customObjectController{


    public static Integer indexGen {get; set;} //index generator
    public List<CustomObjectWrapper> coList;
    public Integer numRows {get; set;}

    
    /*
     * Wrapper class for holding a 'CustomObjects__c' record and its row index together.
     * The row index is used to delete the row using commandLink
     */
    private class CustomObjectWrapper {
        private CustomObjects__c customObject;
        private Integer index;
     
     
     public CustomObjectWrapper() {
            this.customObject = new CustomObjects__c();
            this.index = indexGen++;
        }
        
        public CustomObjects__c getCustomObject() {
            return customObject;
        }
        
        public Integer getIndex() {
            return index;
        }
    }
        
    /*
     * Constructor
     * -----------
     * Initializes 'coList' and 'numRows'
     */
    public customObjectController(ApexPages.StandardController controller) {
        if(indexGen == null) indexGen = 1;
        coList = new List<CustomObjectWrapper>();
        numRows = 1;
    }
    public List<CustomObjectWrapper> getCoList() {
        return coList;
    }


     
    /*
     * upserts all records currently in the 'coList'
     */
    public PageReference save() {
        if(coList.size()==0)
            return new PageReference('/' + ApexPages.currentPage().getParameters().get('retURL'));
        try {
            List<CustomObjects__c> tempList = new List<CustomObjects__c>();
            for(Integer i=0; i<coList.size(); ++i)
                tempList.add(coList[i].getCustomObject());
            upsert(tempList);
            return new PageReference('/' + ApexPages.currentPage().getParameters().get('retURL'));
        } catch (System.DMLException ex) {
            ApexPages.addMessages(ex);
            return null;
        }
    }
    
    
    
     /*
     * appends new records to the 'coList'.
     * The number of records added is determined by the value of 'numRows'
     * Issues with the method: Not working if mandatory fields are left empty
     */
    public void add() {
          try {
            if(numRows > 0)
                for(Integer i=0; i<numRows; ++i)
                    coList.add(new CustomObjectWrapper());
        } catch (Exception ex) {
             ApexPages.addMessages(ex);
        }
   }
    
    
    /*
     * Clears all records from the 'coList'
     * Issues with the method: Not working if mandatory fields are left empty
     */        
    public void clear(){
        coList.clear();
        numRows = 1;
         }
     

    /*
     * deletes a record from 'coList' depending on the 'index' of 'CustomObjects__c' within the 'CustomObjectWrapper' class
     */
     public void delRecords(){
        try {
            Integer delIndex = Integer.valueOf(ApexPages.currentPage().getParameters().get('delRow'));
            
            for(Integer i=0; i<coList.size(); ++i)
                if(coList[i].index == delIndex) {
                    coList.remove(i);
                    break;
                }
        } catch (Exception ex) {
            ApexPages.addMessages(ex);
        }
    }
    
       
             
   }

Test class;

 

@isTest

public class customObjectController_Test{

public static testMethod void testcls(){
CustomObjects__c cust = new CustomObjects__c();
PageReference pageRef = Page.ReportPage;
Test.setCurrentPageReference(pageRef);
ApexPages.StandardController sc = new ApexPages.StandardController(cust);
customObjectController obj = new customObjectController(sc);
customObjectController.CustomObjectWrapper wra = new customObjectController.CustomObjectWrapper();
obj.save();
obj.add();
obj.clear();
obj.delRecords();
wra.getIndex();
wra.getCustomObject();
}
}

alex_from_parisalex_from_paris

Hi

If you are using Eclipse to deploy, it tells you which line has not been covered so you will find the not tested function

Regards

Neha KakrooNeha Kakroo

No i dont have eclipse installed. Doing it directly in the application.

Sandeep001Sandeep001

See if this works:-

 

@isTest

public class customObjectController_Test{

	public static testMethod void testcls(){
		CustomObjects__c cust = new CustomObjects__c();
		
		List<customObjectController.CustomObjectWrapper> coListTest = new List<customObjectController.CustomObjectWrapper>();

		ApexPages.StandardController sc = new ApexPages.StandardController(cust);
		customObjectController obj = new customObjectController(sc);
		obj.save();
		customObjectController.CustomObjectWrapper wra = new customObjectController.CustomObjectWrapper();
		coListTest.add(wra);
		obj.coList = coListTest;
		obj.save();
		obj.add();
		obj.clear();
		ApexPages.currentPage().getParameters().put('delRow', '1');
		obj.delRecords();
		obj.getCoList();
		wra.getIndex();
		wra.getCustomObject();
	}
}

 

You should also make use of System.assert method wherever possible.