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
Ashok S 7Ashok S 7 

how can i check the if else condition

hai,
when i am test class to the controller it is covering only if part and not covering the else part
controller
---------------------
public class customAccountlistcontroller 
{
  public ApexPages.StandardSetController ssc{get; private set;}
  public list<SelectOption> sizeOptions{get;private set;}
  public String chosenSize{get;set;}
  public CustomAccountlistcontroller()
  {
     ssc = new ApexPages.StandardSetController([select id,Name,Rating,NumberOfEmployees from Account limit 500]);
     sizeOptions = new List<SelectOption>();
     {
       new SelectOption('small','small');
       new SelectOption('medium','medium');
       new SelectOption('large','large');
     }
     chosenSize = 'small';
     //ApplyFilter();
   }
   public List<Account> GetAccounts()
   {
      return (List<Account>)ssc.GetRecords();
   }
   
   public Pagereference ApplyFilter()
   {
     String query = 'select id,name,Rating,NumberOfEmployees from Account where';
     if(chosenSize == 'small')
     {
        query += 'Employees <=100';
     }
     else if(chosenSize == 'medium')
     {
       query += 'Employees > 100 and Employees <= 500';
     }
     else
     {
      query += 'Employees > 500';
     }
     query += 'limit 500';
     ssc = new ApexPages.StandardSetController(Database.Query(query));
     return null;
  
   }
}
testclass
----------------------
@isTest
public class Test_customAccountlistcontroller 
{
    public static testmethod void testcustomaccount()
    {
      //using the pagereference
      pagereference pg = page.Advanced_Filtering_vf_page;
        test.setCurrentPage(pg);
        //calling the controller into the testclass
      customAccountlistcontroller cac = new customAccountlistcontroller();
        //calling the methods in the testclass
        cac.GetAccounts();
        cac.ApplyFilter();
        
    }
}
when i run the testclass it is covering the if part but did not cover else part.
following image display the which part is covering and which part is not cover
User-added image
Alexander TsitsuraAlexander Tsitsura
Hi Ashok,

You need create test #2 for coverate if statement and in this test you need set chosenSize as 'medium'

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you.

Thanks,
Alex
 
Jithin Krishnan 2Jithin Krishnan 2
Hi Ashok,
You need add cases for 'medium' and 'large' also. Please try the below class and let me know if you face any issues.
@isTest
public class Test_customAccountlistcontroller 
{
    public static testmethod void testcustomaccount1()
    {
      //using the pagereference
      pagereference pg = page.Advanced_Filtering_vf_page;
        test.setCurrentPage(pg);
        //calling the controller into the testclass
      customAccountlistcontroller cac = new customAccountlistcontroller();
        //calling the methods in the testclass
        cac.GetAccounts();
        cac.ApplyFilter();
    }

    public static testmethod void testcustomaccount2()
    {
      //using the pagereference
      pagereference pg = page.Advanced_Filtering_vf_page;
        test.setCurrentPage(pg);
        //calling the controller into the testclass
      customAccountlistcontroller cac = new customAccountlistcontroller();
        //calling the methods in the testclass
        cac.chosenSize='medium';
        cac.GetAccounts();
        cac.ApplyFilter();
    }
public static testmethod void testcustomaccount3()
    {
      //using the pagereference
      pagereference pg = page.Advanced_Filtering_vf_page;
        test.setCurrentPage(pg);
        //calling the controller into the testclass
      customAccountlistcontroller cac = new customAccountlistcontroller();
        //calling the methods in the testclass 
        cac.chosenSize='large';
        cac.GetAccounts();
        cac.ApplyFilter();
    }
}

Thanks
Jit