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
NNRNNR 

Test class for below Method

Hi All plz tell me how to write test class for bellow method


public PageReference AccNext() {                  if(Account.CIS_No_new__c== Null ){            ApexPages.Message Msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please enter CIS number');            ApexPages.addMessage(msg);             if(!test.isrunningTest())            return null;         }         if(Account.Salutation__c == Null){           ApexPages.Message Msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please enter Salutation for primary customer');            ApexPages.addMessage(msg);                if(!test.isrunningTest())             return null;          }         if(Account.LastName__c== Null){            ApexPages.Message Msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please enter Primary customer Lastname');            ApexPages.addMessage(msg);             if(!test.isrunningTest())            return null;         }                       //====================== new logic added for salutation and gender matching for Primary/Secondary Customer=====================         if(Account.Sex__c != Null && Account.salutation__c != Null && Account.salutation__c == 'Mrs.' && Account.Sex__c == 'M'){            ApexPages.Message Msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please change the Gender to Female under Primary Customer Details');            ApexPages.addMessage(msg);                if(!test.isrunningTest())            return null;                  }         if(Account.Sex__c != Null && Account.salutation__c != Null && Account.salutation__c == 'Mr.' && Account.Sex__c == 'F'){            ApexPages.Message Msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please change the Gender to Male under Primary Customer Details');            ApexPages.addMessage(msg);                if(!test.isrunningTest())            return null;                  }        if(Account.Sex__c != Null && Account.salutation__c != Null && Account.salutation__c == 'Ms' && Account.Sex__c == 'M'){            ApexPages.Message Msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please change the Gender to Female under Primary Customer Details');            ApexPages.addMessage(msg);                if(!test.isrunningTest())            return null;                  }                                    //========================================= end of new logic ===============================================================================================         if(Account.Marital_Status__c== Null ){            ApexPages.Message Msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please enter primary customer marital status');            ApexPages.addMessage(msg);              if(!test.isrunningTest())            return null;                  }          if(Account.Education1__c== Null ){            ApexPages.Message Msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please enter primary customer Education');            ApexPages.addMessage(msg);             if(!test.isrunningTest())             return null;                  }                  if(Account.IdentityProof__c== Null ){            ApexPages.Message Msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please enter primary customer ID type1');            ApexPages.addMessage(msg);              if(!test.isrunningTest())            return null;                  }         if(Account.IdentityProofNo__c== Null ){            ApexPages.Message Msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please enter primary customer Identity No');            ApexPages.addMessage(msg);              if(!test.isrunningTest())           return null;                  }         //updated for new id proof details         if(Account.IdentityProof_coborr__c==Null ){            ApexPages.Message Msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please enter primary customer ID type2');            ApexPages.addMessage(msg);              if(!test.isrunningTest())            return null;                  }         if(Account.IdentityProofNo1__c== Null ){            ApexPages.Message Msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please enter primary customer Identity No2');            ApexPages.addMessage(msg);              if(!test.isrunningTest())           return null;                  }// updation end -         if(Account.Aadhar_Card__c=='Yes' && (Account.Aadhar_Card_No__c==null || Account.Aadhar_Card_No__c=='')){            ApexPages.Message Msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please enter Aadhar card Number');            ApexPages.addMessage(msg);              if(!test.isrunningTest())            return null;                  }         if(Secondary.Name== Null ){            ApexPages.Message Msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please enter Secondary customer First name');            ApexPages.addMessage(msg);              if(!test.isrunningTest())            return null;                  }          if(Secondary.Last_Name__c== Null ){            ApexPages.Message Msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please enter Secondary customer Last name');            ApexPages.addMessage(msg);              if(!test.isrunningTest())            return null;                  }          if(Secondary.Salutation__c == Null)         {           ApexPages.Message Msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please enter Salutation for secondary customer');            ApexPages.addMessage(msg);                if(!test.isrunningTest())             return null;          }          if(Account.Name == Secondary.Name && Account.LastName__c ==   Secondary.Last_Name__c){             ApexPages.Message Msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Primary and Secondary customer name should not be same');            ApexPages.addMessage(msg);              if(!test.isrunningTest())           return null;         }         if(Secondary.Date_of_Birtht__c== Null ){            ApexPages.Message Msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please enter Secondary Customer Date of Birth');            ApexPages.addMessage(msg);              if(!test.isrunningTest())            return null;                  }          if(((Secondary.FH_FirstName__c == Null || Secondary.FH_LastNames__c == Null) && (Secondary.Spouse_First_Name_SC__c== Null || Secondary.Spouse_Last_Name_SC__c == Null)) && Account.Opted_for_NPS_Lite__c == 'No')  {            ApexPages.Message Msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please enter Fathers/Spouse first name and last name of Secondary Customer');            ApexPages.addMessage(msg);                if(!test.isrunningTest())             return null;                  }                  if( (Secondary.FH_FirstName__c== Null || Secondary.FH_LastNames__c == Null)&& Account.Opted_for_NPS_Lite__c == 'Yes'){            ApexPages.Message Msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please enter Fathers first and last name of Secondary customer');            ApexPages.addMessage(msg);                if(!test.isrunningTest())             return null;                  }         /*  Logi for Gender and Salutation  */         if(Secondary.Gender__c != Null && Secondary.Salutation__c != Null && Secondary.Salutation__c == 'Mr.' && Secondary.Gender__c == 'F'){            ApexPages.Message Msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please change the Gender to Male under Secondary Customer Details');            ApexPages.addMessage(msg);             if(!test.isrunningTest())            return null;                     }           if(Secondary.Gender__c != Null && Secondary.Salutation__c != Null && Secondary.Salutation__c == 'Mrs.' && Secondary.Gender__c == 'M'){            ApexPages.Message Msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please change the Gender to Female under Secondary Customer Details');            ApexPages.addMessage(Msg);             if(!test.isrunningTest())            return null;                     }                            if(Secondary.Marital_Statuss__c== Null ){                 ApexPages.Message Msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please select Secondary customer Marital Status');                 ApexPages.addMessage(msg);                 if(!test.isrunningTest())                  return null;             }         */          if(Secondary.Educations__c== Null ){            ApexPages.Message Msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please enter Secondary customer Education');            ApexPages.addMessage(msg);              if(!test.isrunningTest())            return null;                  }         if(Secondary.Identity_Proofs__c== Null ){            ApexPages.Message Msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please enter Secondary customer ID Type');            ApexPages.addMessage(msg);             if(!test.isrunningTest())             return null;                  }         if(Secondary.Identity_ProofNo__c== Null ){            ApexPages.Message Msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please select Secondary customer Identity Proof No');            ApexPages.addMessage(msg);              if(!test.isrunningTest())            return null;                  }          if(Secondary.Gender__c== Null ){            ApexPages.Message Msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please enter Secondary customer SEX');            ApexPages.addMessage(msg);              if(!test.isrunningTest())            return null;                  }         if(Secondary.Nature_of_Employment__c== Null ){            ApexPages.Message Msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please select Secondary customer Nature of Employement');            ApexPages.addMessage(msg);              if(!test.isrunningTest())            return null;                  }          if(Account.Caste__c== Null ){            ApexPages.Message Msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please enter Caste');            ApexPages.addMessage(msg);              if(!test.isrunningTest())            return null;                  }         if(Account.Religion__c== Null ){            ApexPages.Message Msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please select Religion');            ApexPages.addMessage(msg);             if(!test.isrunningTest())             return null;                  }                     else         {                       try{                                  Upsert(Account);                 Account StdaccListfmly = new Account();                             RecordType SecondaryRt= [SELECT DeveloperName,Id,Name FROM RecordType where DeveloperName='For_DEO' and sobjectType='Secondary_Customer_Details__c'];                                  Secondary.RecordtypeId=SecondaryRt.Id;                 StdaccListfmly=[Select id, Name,Age_of_Borrower1__c,Natureofemployment__c from Account where Id =:account.Id limit 1];                 Secondary.Account__c= StdaccListfmly.id;                 upsert(Secondary);//Database.upsert(Secondary); commented by bhanu to catch dml exception - 3 dec                 Secondary_Customer_Details__c SecListfmly = new Secondary_Customer_Details__c();                 SecListfmly=[Select id,Nature_of_Employment__c,Type_of_Relation__c,Length_of_Business_Service_Years__c ,Relationdhip_with_Secondary_Customer__c,Name,Age_of_Co_Borrower__c from Secondary_Customer_Details__c where Id =:Secondary.Id limit 1];                 system.debug('**********Relationdhip_with_Secondary_Customer__c***********'+SecListfmly.Relationdhip_with_Secondary_Customer__c);                 system.debug('**********Type_of_Relation__c***********'+SecListfmly.Type_of_Relation__c);                 String nextTabValue = 'ContactDetails';                   if(nextTabValue != null){                    currentTab = nextTabValue;                         Opportunity.Account__c= account.id;                      Opportunity.Secondary_Customer_Details__c=secondary.Id;                                                          }            }            catch(DmlException e){                 ApexPages.addMessages(e);                 LogsExceptionClass.insertLog(e.getMessage(),e.getLineNumber(),e.getTypeName(),String.valueOf(e.getcause()),e.getStackTraceString());            }        }        return null;     }

 
Best Answer chosen by NNR
JWykelJWykel

So.many.if.statements!  And not formatted! (Use the code sample button when posting code, looks like < > in the bar above the edit window)

First, some suggestions:
Use String.isBlank(...) for checking if a field is filled in; it will check for both null as well empty/whitespace strings.
Get rid of the if(!test.isRunningTest()) return null calls, they'll just end up being confusing when trying to step through the logic.
Shorten up/make it a bit more readable by splitting repeated functionality out into a method, something like:
 

private void AddError(String errorMessage){
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, message);
}

public PageReference AccNext(){
    if(String.isBlank(Account.CIS_No_new__c)) AddError('Error: Please enter CIS number');
    if(String.isBlank(Account.Salutation__c)) AddError('Error: Please enter a Salutation for primary customer');
    if(...)
    
    if(!ApexPages.hasMessages(ApexPages.Severity.ERROR)){
        DoYourStuffIfEverythingIsValidated();
    }
}
 


To answer your question:
 

MyController cont = new MyController();

Test.startTest();

cont.AccNext(); //This should cause ApexPages to get filled with *all* of your error messages

cont.Account.CIS_No_new__c = 'something';
cont.Account.Salutation__c = 'something';
... // Fill in all of the fields you're checking for
cont.AccNext(); //This should run your code since all of the validation points are covered.

Test.stopTest();
I'll leave it up to you to infer what the above means and how to make it work and cover all parts of the class as well as making it actually 'test' something.

All Answers

JWykelJWykel

So.many.if.statements!  And not formatted! (Use the code sample button when posting code, looks like < > in the bar above the edit window)

First, some suggestions:
Use String.isBlank(...) for checking if a field is filled in; it will check for both null as well empty/whitespace strings.
Get rid of the if(!test.isRunningTest()) return null calls, they'll just end up being confusing when trying to step through the logic.
Shorten up/make it a bit more readable by splitting repeated functionality out into a method, something like:
 

private void AddError(String errorMessage){
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, message);
}

public PageReference AccNext(){
    if(String.isBlank(Account.CIS_No_new__c)) AddError('Error: Please enter CIS number');
    if(String.isBlank(Account.Salutation__c)) AddError('Error: Please enter a Salutation for primary customer');
    if(...)
    
    if(!ApexPages.hasMessages(ApexPages.Severity.ERROR)){
        DoYourStuffIfEverythingIsValidated();
    }
}
 


To answer your question:
 

MyController cont = new MyController();

Test.startTest();

cont.AccNext(); //This should cause ApexPages to get filled with *all* of your error messages

cont.Account.CIS_No_new__c = 'something';
cont.Account.Salutation__c = 'something';
... // Fill in all of the fields you're checking for
cont.AccNext(); //This should run your code since all of the validation points are covered.

Test.stopTest();
I'll leave it up to you to infer what the above means and how to make it work and cover all parts of the class as well as making it actually 'test' something.
This was selected as the best answer
NNRNNR
Thank you so much JacobLabrix,it is working..