+ Start a Discussion
SFDC@ErrorSFDC@Error 

Improve code

Hi All How can i improve my code coverage .Now it is 65%
public class AddCostCenterController 
{
	Id targetid;
    public List<Cost_Center__c> cst {set;get;}
    public integer rno {set;get;}
    
    public AddCostCenterController(ApexPages.StandardController controller)
    {
        targetid =Apexpages.currentPage().getparameters().get('targetid');
        cst = new List<Cost_Center__c>();
        Cost_Center__c ct= new Cost_Center__c();
        ct.Target__c = targetid;
        cst.add(ct);
    }
    public pageReference save(){
        insert cst;
        pageReference ct = new PageReference('/'+targetid);
        return ct;
    
    }
    public void addRow()
    {
        cst.add(new Cost_Center__c(Target__c = targetid ));
        
    }
    public void delRow(){
        rno = integer.valueOf(Apexpages.currentPage().getParameters().get('index'));
        cst.remove(rno);
    }
}
 
@isTest
private class Ctr_AddCostCenters_Test
{
  static testMethod void Ctr_AddCostCenters(){
    test.startTest();
    
    
    
     Goal__c goal_Obj = new Goal__c(Name='Test',Flag__c = false,Financial_Year__c='2018-2019');
    Insert goal_Obj; 
      
    Cost_Center__c cost_center_Obj = new Cost_Center__c(Target__c = goal_Obj.id);
    Insert cost_center_Obj; 
    
    PageReference pageRef = Page.AddcostCenters;
    pageRef.getParameters().put('cost_center_Obj','test');
    pageRef.getParameters().put('index','test');
    Test.setCurrentPage(pageRef);
   ApexPages.StandardController sc = new ApexPages.StandardController(cost_center_Obj);
   AddCostCenterController obj01 = new AddCostCenterController(sc);  
   obj01.addRow();
   obj01.save();
   obj01.delRow();
    
    
              
      
  }
 
}

 
Siddharth SethSiddharth Seth
Hi SFDC@Error

You can doble click on the test class in the test panel in you developer console to check which all lines are covered. And then accordingly modify you code.
Or provide with those details here.


Best Regards,
Siddharth S.
AppPerfect Corp.
salesforce@appperfect.com (mailto:salesforce@appperfect.com)
408-252-4100
http://www.appperfect.com/salesforce/

 
SFDC@ErrorSFDC@Error
I have checked bellow line are not covered insert cst; pageReference ct = new PageReference('/'+targetid); return ct; } public void addRow() { cst.add(new Cost_Center__c(Target__c = targetid )); } public void delRow(){ rno = integer.valueOf(Apexpages.currentPage().getParameters().get('index')); cst.remove(rno); } }
AmitdAmitd
Hi SFDC,

Please make follwoing changes to your test calss and it should cover the remaining lines

Delete follwoing line 
pageRef.getParameters().put('cost_center_Obj','test');

Add following line

pageRef.getParameters().put('targetid',goal_Obj.id);

Change following line 
pageRef.getParameters().put('index','test');

with 
pageRef.getParameters().put('index',0);