+ Start a Discussion
gomennasai123@n.orggomennasai123@n.org 

How do I unit test if else branches in my controller

I have a controller that has the following lines of code. (abbreviated for example purposes)

 

public  with sharing class  myReferralController2  {

public string   CaseAppMonth(string mymonth) {
  string mynumbermonth;
 
   if    (mymonth == 'Jan') {
          mynumbermonth ='1';    }
      else
       if (mymonth == 'Feb') {
           mynumbermonth ='2';    }
      else
       if  (mymonth == 'Mar') {
            mynumbermonth ='3';    }
    if      (mymonth == 'Apr') {
           mynumbermonth ='4';    }
      else
       if (mymonth == 'May') {
          mynumbermonth ='5';    }
      else
       if (mymonth == 'Jun') {
           mynumbermonth ='6';    }
      else
       if (mymonth == 'Jul') {
          mynumbermonth ='7';    }
      else
       if (mymonth == 'Aug') {
           mynumbermonth ='8';    }
       else
       if (mymonth == 'Sep') {
           mynumbermonth ='9';    }
      else
       if  (mymonth == 'Oct') {
            mynumbermonth ='10';    }
      if   (mymonth == 'Nov') {
            mynumbermonth ='11';    }
      if   (mymonth == 'Dec') {
            mynumbermonth ='12';    }
      
 
   return mynumbermonth;                       }

 

 

 

my unit test is something like this:

 

myReferralController2 cc1 = new  myReferralController2();
              cc1.NewReferralClientc.Client_Rep_ID__c='DEC79EN';
           .      .
.           cc1.getChooseReferral();
           cc1.ReferralType='Engagement';
           List<SelectOption> ChoosePartner =    cc1.getChoosePartner();
           cc1.PartnerProviderType = 'TNCSA';
           List <string> appointmentlist1 = cc1.getlistappointments();
                                            cc1.getDateAppointmentsListValue();
         list <SELECTOPTION> options = cc1.getDateAppointmentsoptions();
                                     cc1.AddDateTime();
                 
                                    cc1.IsDateAndTimeSelected();
                                    cc1.norefresh();
                               
           cc1.NewReferralClientc.Client_Rep_ID__c  = 'DEC79EN';
           cc1.ClientFirstName  = 'Bob';
           cc1.ClientLastName = 'Smith';
           cc1.ClientLastFour ='0900';
           cc1.ClientPhone ='2152345677';
               
        
           string numbermonth = cc1.CaseAppMonth('Jun');
           system.assertequals('6',numbermonth);
         

 

When I  run the Unit test I don't see any increase in coverage.

Can you show me how to cover the if else branch and how to call caseappMonth?

 

Thank You

 

 

 

 

 

 

Jake GmerekJake Gmerek

 

You have to repeat the test for each case( ie. Jan, Feb, etc.).

asish1989asish1989

hi 

    Try this

      

     string numbermonth = cc1.CaseAppMonth('Jan');
           system.assertequals('1',numbermonth);

 

    string numbermonth = cc1.CaseAppMonth('Feb');
           system.assertequals('2',numbermonth);

  Like wise repeat all the month.Your code coverage will be above 90%

 

   

Did this post solve your problem...? If so please mark it solved

Thanks

asish