+ Start a Discussion
revathyrevathy 

how to coverage test class for more if/else statement?

i have provided Test class & Apex class and code coverage test screen.
======apex class=========
 //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')){
          // }else if((acc.Rfleet_UpperMonth_Period1__c=='M') && (acc.Rfleet_LowerMonth_Period1__c.contains('M + 1,M + 2,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(seealldata=true)
private class Rfleet_PaymentConditions_Test{
   static testmethod void testpayment(){
        account acc = new account(name='jira',montant__c=0.5);
        insert acc;
        acc.name='raji';
        acc.Rfleet_LowerDate_Period1__c='15';
        acc.Rfleet_UpperDate_Period1__c='10';
        update acc;
       // Test.startTest();
        PageReference tpageRef = Page.RFLEET_PaymetCondition;
        Test.setCurrentPage(tpageRef);
        ApexPages.currentPage().getParameters().put('Id', acc.Id);
        System.assertEquals( acc.id,ApexPages.currentPage().getParameters().get('Id'));
        
        Rfleet_PaymentConditions c = new Rfleet_PaymentConditions(new ApexPages.StandardController(acc));
        c.getAccount();
        c.save(); 
        c.back();
        c.Submit();
        c.ResetPayment();
        c.bool=true; 
        c.ResetDepreciationPayment();
         System.assertEquals(true, c.bool);  
        c.Period1LowerDateValidation();
        c.Period1UpperDateValidation();       
       // Test.stopTest();
        
       
    }
 }

=====Screen shot for code coverage======
User-added image
 
Best Answer chosen by revathy
@Karanraj@Karanraj
You have to set the value in the test class data which statisfy the IF condition. To cover the IF condition, you need to set Rfleet_LowerDate_Period1__c value should be greater than the Rfleet_UpperDate_Period1__c field value. More importantly don't use seealldata = true in your test class. Hope that will solve your problem