1

I am new for test classes even I written test class it's covered 34% but I need to increase code coverage. Please can any one help me out how to increase my code coverage, please find below my apex class,test class and error message.

public class Rfleet_PaymentConditions {
public Boolean bool {get;set;}
public Boolean boolsave {get;set;}
public Boolean showSave{get;set;}
public Boolean showSubmit{get;set;}
public Boolean showback{get;set;}
public string id;
public Account acc{get;set;}
public Boolean refreshPage {get; set;}
 public Account getAccount() {
   return acc;
}

 public Rfleet_PaymentConditions(ApexPages.StandardController controller){
   bool = true;
   showSave = true;
   id=ApexPages.currentPage().getParameters().get('id');
    acc=[SELECT Rfleet_UpperDate_Period1__c,Rfleet_UpperMonth_Period1__c,Rfleet_LowerDate_Period1__c,Rfleet_LowerMonth_Period1__c,Rfleet_DueDate_Period1__c,Rfleet_DueMonth_Period1__c,Rfleet_UpperDate_Period2__c,Rfleet_UpperMonth_Period2__c,Rfleet_LowerDate_Period2__c,Rfleet_LowerMonth_Period2__c,Rfleet_DueDate_Period2__c,Rfleet_DueMonth_Period2__c,
   Rfleet_Depreciation_UpperDate_Period1__c,Rfleet_Depreciation_UpperMonth_Period1__c,Rfleet_Depreciation_LowerDate_Period1__c,Rfleet_Depreciation_LowerMonth_Period1__c,Rfleet_Depreciation_DueDate_Period1__c,Rfleet_Depreciation_DueMonth_Period1__c,Rfleet_Depreciation_UpperDate_Period2__c,Rfleet_Depreciation_UpperMonth_Period2__c,Rfleet_Depreciation_LowerDate_Period2__c,
Rfleet_Depreciation_LowerMonth_Period2__c,Rfleet_Depreciation_DueDate_Period2__c,Rfleet_Depreciation_DueMonth_Period2__c,Event_Triggering_Billing__c,Rfleet_Billing_Type__c,Rfleet_Billing_Delay__c FROM Account where id=:id];

    }

public void ResetPayment(){
 acc.Rfleet_UpperDate_Period1__c='';
 acc.Rfleet_UpperMonth_Period1__c='';
  acc.Rfleet_LowerDate_Period1__c='';
 acc.Rfleet_LowerMonth_Period1__c='';
 acc.Rfleet_DueDate_Period1__c='';
 acc.Rfleet_DueMonth_Period1__c='';
 acc.Rfleet_UpperDate_Period2__c='';
 acc.Rfleet_UpperMonth_Period2__c='';
 acc.Rfleet_LowerDate_Period2__c='';
 acc.Rfleet_LowerMonth_Period2__c='';
 acc.Rfleet_DueDate_Period2__c='';
 acc.Rfleet_DueMonth_Period2__c='';
 acc.Event_Triggering_Billing__c='';
 acc.Rfleet_Billing_Type__c='';
 acc.Rfleet_Billing_Delay__c=0;
}
public void ResetDepreciationPayment(){
  acc.Rfleet_Depreciation_UpperDate_Period1__c='';
acc.Rfleet_Depreciation_UpperMonth_Period1__c='';
acc.Rfleet_Depreciation_LowerDate_Period1__c='';
acc.Rfleet_Depreciation_LowerMonth_Period1__c='';
acc.Rfleet_Depreciation_DueDate_Period1__c='';
acc.Rfleet_Depreciation_DueMonth_Period1__c='';
acc.Rfleet_Depreciation_UpperDate_Period2__c='';
acc.Rfleet_Depreciation_UpperMonth_Period2__c='';
acc.Rfleet_Depreciation_LowerDate_Period2__c='';
  acc.Rfleet_Depreciation_LowerMonth_Period2__c='';
  acc.Rfleet_Depreciation_DueDate_Period2__c='';
  acc.Rfleet_Depreciation_DueMonth_Period2__c='';
 }
public void save(){
  bool =false;
  boolsave=false;
  showSave=false;
  showSubmit=true;
  showback=true;
 }
public PageReference back(){
 refreshPage=true; 
 return null;
} 
   public PageReference Submit() { 
    if((acc.Rfleet_UpperDate_Period1__c==Null ||acc.Rfleet_UpperMonth_Period1__c==Null||acc.Rfleet_LowerDate_Period1__c==Null||acc.Rfleet_LowerMonth_Period1__c==Null||acc.Rfleet_DueDate_Period1__c==Null||acc.Rfleet_DueMonth_Period1__c==Null||acc.Rfleet_UpperDate_Period2__c==Null||acc.Rfleet_UpperMonth_Period2__c==Null||acc.Rfleet_LowerDate_Period2__c==Null||acc.Rfleet_LowerMonth_Period2__c==Null||acc.Rfleet_DueDate_Period2__c==Null||acc.Rfleet_DueMonth_Period2__c==Null)||  (acc.Rfleet_Depreciation_UpperDate_Period1__c==null ||acc.Rfleet_Depreciation_UpperMonth_Period1__c==null ||acc.Rfleet_Depreciation_LowerDate_Period1__c==null ||acc.Rfleet_Depreciation_LowerMonth_Period1__c==null ||acc.Rfleet_Depreciation_DueDate_Period1__c==null ||acc.Rfleet_Depreciation_DueMonth_Period1__c==null ||acc.Rfleet_Depreciation_UpperDate_Period2__c==null ||acc.Rfleet_Depreciation_UpperMonth_Period2__c==null ||acc.Rfleet_Depreciation_LowerDate_Period2__c==null ||acc.Rfleet_Depreciation_LowerMonth_Period2__c==null ||acc.Rfleet_Depreciation_DueDate_Period2__c==null ||acc.Rfleet_Depreciation_DueMonth_Period2__c==null)){ 
    boolsave=true; 
    Apexpages.addmessage(new Apexpages.Message(ApexPages.Severity.Warning,'please must fill all Terms of Payment fields!'));
  }else{    
    update acc;
    Apexpages.addmessage(new Apexpages.Message(ApexPages.Severity.INFO,'Record saved Successfully!'));
   refreshPage=true;
}
return null;
}

//period1 LowerDate validation 
 public PageReference Period1LowerDateValidation(){

   if(acc.Rfleet_LowerDate_Period1__c!=Null && acc.Rfleet_LowerMonth_Period1__c != Null && acc.Rfleet_UpperMonth_Period1__c!=null && acc.Rfleet_UpperDate_Period1__c !=null ){
     boolsave=false;
      if((Integer.valueof(acc.Rfleet_LowerDate_Period1__c)> Integer.valueof(acc.Rfleet_UpperDate_Period1__c)) && (acc.Rfleet_LowerMonth_Period1__c==acc.Rfleet_UpperMonth_Period1__c)){
            boolsave=true;             
            ApexPages.Message msg = new Apexpages.Message(ApexPages.Severity.Warning,'Please select the past Date of lowerdate in Period1!!');
            ApexPages.addmessage(msg);

      }else if((Integer.valueof(acc.Rfleet_LowerDate_Period1__c)== Integer.valueof(acc.Rfleet_UpperDate_Period1__c)) && (acc.Rfleet_LowerMonth_Period1__c==acc.Rfleet_UpperMonth_Period1__c)){
            boolsave=true;
            ApexPages.Message msg = new Apexpages.Message(ApexPages.Severity.Warning,'Should not same lowerdate/UpperDate in Period1!!');
            ApexPages.addmessage(msg);

      }else if((acc.Rfleet_UpperMonth_Period1__c=='M') &&(acc.Rfleet_LowerMonth_Period1__c=='M + 1'||acc.Rfleet_LowerMonth_Period1__c=='M + 2'|| acc.Rfleet_LowerMonth_Period1__c=='M + 3')){
            boolsave=true;
            ApexPages.Message msg = new Apexpages.Message(ApexPages.Severity.Warning,'Please select the past month LowerDate in Period1!!');
            ApexPages.addmessage(msg);

      }else if((acc.Rfleet_UpperMonth_Period1__c=='M + 1') &&(acc.Rfleet_LowerMonth_Period1__c=='M + 2'|| acc.Rfleet_LowerMonth_Period1__c=='M + 3')){
            boolsave=true;
            ApexPages.Message msg = new Apexpages.Message(ApexPages.Severity.Warning,'Please select the past month LowerDate in Period1!!');
            ApexPages.addmessage(msg);

      }else if(acc.Rfleet_UpperMonth_Period1__c=='M + 2' && acc.Rfleet_LowerMonth_Period1__c=='M + 3'){
            boolsave=true;
            ApexPages.Message msg = new Apexpages.Message(ApexPages.Severity.Warning,'Please select the past month LowerDate in Period1!!');
            ApexPages.addmessage(msg);

      }else if((acc.Rfleet_UpperMonth_Period1__c=='M - 1') &&(acc.Rfleet_LowerMonth_Period1__c=='M'|| acc.Rfleet_LowerMonth_Period1__c=='M + 1'|| acc.Rfleet_LowerMonth_Period1__c=='M + 2'|| acc.Rfleet_LowerMonth_Period1__c=='M + 3')){
            boolsave=true;
            ApexPages.Message msg = new Apexpages.Message(ApexPages.Severity.Warning,'Please select the past month LowerDate in Period1!!');
            ApexPages.addmessage(msg);

      }else if((acc.Rfleet_UpperMonth_Period1__c=='M - 2') &&(acc.Rfleet_LowerMonth_Period1__c=='M - 1'|| acc.Rfleet_LowerMonth_Period1__c=='M'|| acc.Rfleet_LowerMonth_Period1__c=='M + 1'|| acc.Rfleet_LowerMonth_Period1__c=='M + 2'|| acc.Rfleet_LowerMonth_Period1__c=='M + 3')){
           boolsave=true;
            ApexPages.Message msg = new Apexpages.Message(ApexPages.Severity.Warning,'Please select the past month LowerDate in Period1!!');
            ApexPages.addmessage(msg);

      }else if((acc.Rfleet_UpperMonth_Period1__c=='M - 3') &&(acc.Rfleet_LowerMonth_Period1__c=='M - 1'|| acc.Rfleet_LowerMonth_Period1__c=='M - 2'|| acc.Rfleet_LowerMonth_Period1__c=='M'|| acc.Rfleet_LowerMonth_Period1__c=='M + 1'|| acc.Rfleet_LowerMonth_Period1__c=='M + 2'|| acc.Rfleet_LowerMonth_Period1__c=='M + 3')){
           boolsave=true;
            ApexPages.Message msg = new Apexpages.Message(ApexPages.Severity.Warning,'Please select the past month LowerDate in Period1!!');
            ApexPages.addmessage(msg);

      }

  }
   return null;
}   

Test class:

 @isTest
 public class Rfleet_PaymentConditions_Test {
   static testMethod void testpayment(){
    Account acct = new   Account(Name='kathir',Montant__c=5,Rfleet_UpperDate_Period1__c='21',Rfleet_UpperMonth_Period1__c='M + 1',Rfleet_LowerDate_Period1__c='21',
                                Rfleet_LowerMonth_Period1__c='M - 1',Rfleet_DueDate_Period1__c='21',Rfleet_DueMonth_Period1__c='M + 2',Rfleet_UpperDate_Period2__c='21',
                                Rfleet_UpperMonth_Period2__c='M + 3',Rfleet_LowerDate_Period2__c='21',Rfleet_LowerMonth_Period2__c='M - 2',Rfleet_DueDate_Period2__c='21',
                                Rfleet_DueMonth_Period2__c='M + 3',Rfleet_Depreciation_UpperDate_Period1__c='21',Rfleet_Depreciation_UpperMonth_Period1__c='M + 1',
                                Rfleet_Depreciation_LowerDate_Period1__c='21',Rfleet_Depreciation_LowerMonth_Period1__c='M - 3',
                                Rfleet_Depreciation_DueDate_Period1__c='21',Rfleet_Depreciation_DueMonth_Period1__c='M + 2',Rfleet_Depreciation_UpperDate_Period2__c='21',
                                Rfleet_Depreciation_UpperMonth_Period2__c='M + 1',Rfleet_Depreciation_LowerDate_Period2__c='21',
                                Rfleet_Depreciation_LowerMonth_Period2__c='M - 3',Rfleet_Depreciation_DueDate_Period2__c='21',Rfleet_Depreciation_DueMonth_Period2__c='M + 3');
    insert acct;
    acct.Name='joshua';
    update acct;
    Account paytest = [Select id,Name,Rfleet_UpperMonth_Period1__c from account LIMIT 100];
    pagereference vfpage = page.RFLEET_PaymetCondition;
    system.Test.setCurrentPage(vfpage);
    ApexPages.currentPage().getParameters().put('id', paytest.id);
    apexpages.StandardController sc = new apexpages.StandardController(paytest);
    Rfleet_PaymentConditions pay = new Rfleet_PaymentConditions(sc);
    pay.save();
    pay.back();
    pay.getAccount();
    pay.boolsave=true;
    pay.Submit();
    pay.Period1LowerDateValidation();
    pay.Period1UpperDateValidation();
    pay.Period1DueDateValidation();
    pay.Period2LowerDateValidation();
    pay.Period2UpperdateValidation();
    pay.Period2DuedateValidation();
    pay.DepreciationPeriod1LowerDateValidation();
    pay.DepreciationPeriod1UpperDateValidation();
    pay.DepreciationPeriod1DueDateValidation();
    pay.DepreciationPeriod2UpperDateValidation();
    pay.DepreciationPeriod2LowerDateValidation();
    pay.DepreciationPeriod2DueDateValidation();
    pay.ResetPayment();
    pay.ResetDepreciationPayment();
  }
}

Error line images: enter image description here

2
  • Are you just trying to get better code coverage, or do you want to right tests that are actually useful? Currently, you're not actually testing anything. You're just writing a test method that happens to cover a proportion of your code.
    – Nick C
    Commented Nov 18, 2015 at 3:45
  • Hi Nick Cook thanks for your response I am new for test class please could you suggest me what do I need to change my code.
    – Jansi
    Commented Nov 18, 2015 at 4:17

2 Answers 2

1

You should write your test code to perform assertions not simply just hit the code to make the SF deployment process happy. And assert corner/edge cases too, not just the happy path scenario(s).

Great place to start

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm

But to answer your question, you need to create the scenarios you are checking for in your if/else statements with your data.

0

In addition to the above (can't say enough about asserts), you can simplify you code so there aren't so many paths to test. For example, this if statement and the many like it:

if((acc.Rfleet_UpperMonth_Period1__c=='M - 3') &&
  (acc.Rfleet_LowerMonth_Period1__c=='M - 1'|| 
   acc.Rfleet_LowerMonth_Period1__c=='M - 2'|| 
   acc.Rfleet_LowerMonth_Period1__c=='M'|| 
   acc.Rfleet_LowerMonth_Period1__c=='M + 1'|| 
   acc.Rfleet_LowerMonth_Period1__c=='M + 2'|| 
   acc.Rfleet_LowerMonth_Period1__c=='M + 3'))

could be replaced by

if((acc.Rfleet_UpperMonth_Period1__c=='M - 3') &&
    ('M - 3:M - 2:M - 1:M + 1:M + 2:M + 3'.contains(acc.Rfleet_LowerMonth_Period1__c) || 
    acc.Rfleet_LowerMonth_Period1__c=='M'))

and this just begins to get you thinking about other ways to use declarations to define your rules and then using methods or for loops to process input data against the declarations.

For example, with methods, you could have a method called isInvalidUpperAndLower defined as:

private Boolean isInvalidUpperAndLower(String upper, Set<String> upperSet, String lower, Set<String> lowerSet {
    return upperSet.contains(upper) && lowerSet.contains(lower);

and you invoke by

if (isInvalidUpperAndLower(acc.Rfleet_UpperMonth_Period1__c, new Set<String>{'M - 3'}, 
    acc.Rfleet_LowerMonth_Period1__c, new Set<String> {'M - 1','M - 2', 'M', 'M + 1', 'M + 2', 'M + 3'}) {
...
}

You can take it a step further and place the values of the sets in static resources or custom settings so you can change the rules without having to change the code if you think you will go to M + 4 and M - 4.

All food for thought

Bottom line, when I see so many if-elses, I'd look for ways to declare the rules and then use minimal code. This simplifies code coverage and makes assertions less voluminous.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .