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
ttlttl 

Test method covering only 59% for controller extension

Hi Guys,

 

I have one controller which is working fine in sandbox now i want to deploy it to production,I write test method for this controller but the test coverge is only 59%. Guys I am not finding the way to write the test method which increase the  test coverage  for the controller .My controller code is as follow.

I darked the code which is not covered during execution.

 

public class Accountscorecard {

public String str{get;set;}

public List<Account_Spend__c> accountspend{get;set;}

Account_Spend__c objaccountspend{get;set;}

pagereference p1{get;set;}

 

public Accountscorecard(ApexPages.StandardController controller){

      str = ApexPages.currentPage().getParameters().get('id');

      accountspend=new List<Account_Spend__c>();

      accountspend=[select Account__c,Service_Provider__c,wireline_voice__c,wireless_data__c from Account_Spend__c where Account__c=:str];

     }

 

public Account_Spend__c getObjaccountspend(){

     if(objaccountspend==null){

      objaccountspend=new Account_Spend__c();

       objaccountspend.Account__c=str;

     }

     return objaccountspend;   

     }

 

public pageReference accountspendentry(){

    Account_Spend__c[] accspend= [select id from Account_Spend__c where Account__c=:objaccountspend.Account__c and Service_Provider__c = :objaccountspend.Service_Provider__c]; 

      if(objaccountspend.Service_Provider__c== null){

           objaccountspend.Service_Provider__c.addError('Please select the service provider,you cannot leave it Blank.');

          p1=page.accountspendentry;

          }

//guys this below part is not covering during execution

           else{

               if (accspend.size() > 0 ) {      

                objaccountspend.Service_Provider__c.addError(' This Service Provider already exists  with  this company.');

                p1=page.accountspendentry;

                }else{

                      try{

                          insert objaccountspend;

 

                         }catch(System.DMLException e) {

                               ApexPages.addMessages(e);

                                return null;

                         }

 

       this.accountspend=[select Account__c,Service_Provider__c,wireline_voice__c,wireless_data__c from Account_Spend__c where Account__c=:str];

 

  //this is update method which is called when any new record inserted and new updated value will display on the screen.

 

      accountspendupdate();

 

       p1=page.detailvf;

       p1.setRedirect(true);

       p1.getParameters().put('id',str);

 

       }

     }

      return p1;

  }

 

public pageReference accountspendupdate(){ 

    Double j1=0;Double j2=0;

    try{  update accountspend;

     }catch(System.DMLException e) {

      ApexPages.addMessages(e);

      return null;

      }

   this.accountspend=[select Account__c,Service_Provider__c,wireline_voice__c,wireless_data__c from Account_Spend__c where Account__c=:str];

    for(Account_Spend__c accspend:accountspend){

        j1+= accspend.wireline_voice__c ;

        j2+=accspend.wireless_data__c;

         }

 

      AccountCard__c acccard =[select wireline_voice__c,wireless_data__c from AccountCard__c where Account__c=:str Limit 1];

       acccard.Wireline_Voice__c=j1;acccard.Wireless_Data__c=j2;

       update acccard; 

     return page.detailvf;

 

    }

// test method……

static testMethod void accountscorecard_test(){

 

    Account acc= new Account(Name='Tata',RecordTypeId='012N00000004HgH');

    insert acc;

 

         Account_Spend__c accspend1= new Account_Spend__c (Name='Tcs',Account__c =acc.Id,   Service_Provider__c='TTL',  wireline_voice__c=1,wireless_data__c=1);

 

     insert accspend1;

 

  ApexPages.StandardController sc = new ApexPages.StandardController(acc);

   PageReference viewPageRef = Page.detailvf;

   Test.setCurrentPage(viewPageRef);

    System.CurrentPageReference().getParameters().put('id', acc.id);

    Accountscorecard controllerobj = new Accountscorecard(sc);

    controllerobj.getObjaccountspend();

     controllerobj.accountspendupdate();

     controllerobj.accountspendentry();

    }

 

}

 

 

 

 

Please help me guys to complete my test coverage ,it's urgent for me to deploy it .

Any suggestion would be heartly appreciated.

 

Thanks,

Prince

sfdcfoxsfdcfox

You need to test your "else" branch. Typically, this means that you'd use a second testMethod that purposefully causes the operation to fail. The hardest part of testing is to consider all possible scenarios and write a test that "proves" that each scenario works. The specific actions you need to take depend on what you're testing. In this case, you need to insert records to cause that condition to branch, and then run a test against it.

 

In practice, if you can't reach a particular piece of code through a test method, you probably need to reevaluate how the code is structured in order to make the branch testable. The sole exception to this case should be truly untestable scenarios (i.e. you have a callout method, so you have to simulate, or you code for a highly unlikely branch of execution). There is a reason why code coverage requirements is only 75%; the developers realize that you can't always test every conceivable branch. However, forcing at least a majority of the code coverage not only helps eliminate errors, but proves that you at least understand what the code is doing.

 

Also, just an aside, make sure you're running the appropriate tests during testing and deployment. I find that sometimes I need to turn off or turn on "run all tests" in order to make a deployment work.

ttlttl

Thanks guys, for instantly reply,could you please share a bit of  test method code for my scenario, so that i  can procced through that approach.