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
❤Code❤Code 

need help in writing test class

Hi ,

I need help in writng a test class class for the below controller - 

public with sharing class Project_Report {

public string model{get;set;}
public List<String> SelectedDept{get;set;}
public List<String> SelectedName{get;set;}
public List<SelectOption> AllDept{get;set;}
public List<SelectOption> AllName{get;set;}
public set<String> sSelectedDept;
public set<String> sSelectedProj;
public List<Project__c> results{get;set;}



public Project_Report()
{
 AllDept= new List<SelectOption>();
 AllDept= getDept();
 AllName= new List<SelectOption>();
 AllName= getName();

}

 public List<SelectOption> getDept()
    {
        List<SelectOption> options = new List<SelectOption>();
        Schema.DescribeFieldResult departfield =  Project__c.Department_Name__c.getDescribe();
        List<Schema.PicklistEntry> picklist = departfield.getPicklistValues();
        for( Schema.PicklistEntry f : picklist){      
        options.add(new SelectOption(f.getLabel(), f.getValue()));
        }    
        return options;
        
     }

       public List<SelectOption> getName()
    {
        List<SelectOption> options = new List<SelectOption>();
        Set<String> sProj = new Set<String>();
        
        if(SelectedDept != null && SelectedDept.size()>0)
            {
            system.debug('@@@@'+SelectedDept);
               for(String oSelectOption : SelectedDept)
               {
                    sProj.add(oSelectOption);
               }
               for(Project__c Proj  :[SELECT ID,Name FROM Project__c Where Department_Name__c IN : sProj] )
               {
                    options.add(new SelectOption(Proj.ID,Proj.Name));
               }
                
            }
            else
            {
                
                for(Project__c Proj  :[SELECT ID,Name FROM Project__c ] )
                {
                   options.add(new SelectOption(Proj.ID,Proj.Name));
                }
                
            }
      
        return options;
        
     }

    public void FindAllName()
    {
        system.debug('@@@Inside find all name');
        AllName= new List<SelectOption>();
        results = new List<Project__c>();        
        AllName= getName();
        
    }
    public void doSearch(){
    results = new List<Project__c>();   
    sSelectedDept = new set<String>();
    sSelectedProj = new set<String>(); 
    
    if(SelectedDept != null && SelectedDept.size()>0){
     for(String option: SelectedDept)
            {
              sSelectedDept.add(option);
            }
         }
          else
            {
                for(SelectOption oSelectOption : AllDept)
                {
                   sSelectedDept.add(oSelectOption.getValue());
                }
            }
            
    if(SelectedName !=null && SelectedName.size()>0){
    for(String options:SelectedName)
            {
              sSelectedProj.add(options);
            }
        }
        else
            {
                for(SelectOption oSelectOptions : AllName)
                {
                
                sSelectedProj.add(oSelectOptions.getValue());
                
                }
            }
            system.debug('$$$$$$' + sSelectedProj);
            String strProjqry  = 'select Id, Name,Project_Manager__c,Start_Date__c,End_Date__c,Status__c,Department_Name__c from Project__c ';
            
            if(sSelectedDept !=null && sSelectedDept.size()>0)
                {
                    strProjqry  += ' Where Department_Name__c IN :sSelectedDept';
                }
                

            if(sSelectedProj !=null && sSelectedProj.size()>0)
                {
                    strProjqry  += ' and ID IN: sSelectedProj' ;
                }

            System.debug('@@@Query:'+strProjqry);
            try
           {
                  results= database.query(strProjqry)  ;
                  system.debug('@@@@'+results.size());
                  
            }
            catch(Exception ex)
            {
                System.debug('This is the bug :'+ex);
            }
    }

}
Regards

 
Yogesh KulkarniYogesh Kulkarni
Hi,

Create a test class with test method which will instantiate this particular controller. Then when you use the default constructor it will automatically call few methods in your controller. After that for all the conditional statement code coverage populate the values for your atttribites (Pass in values for SelectedDept) and then call all the methods one by one. This will make sure your code coverage also for methods like GetDept you can verify the recult with assertion.

Thanks,
Yogesh
❤Code❤Code
Hi Yogesh,

I created a sample for the above code . It is covering 71%. Can u help me in getting 90 % coverage. What else i need to do.

@isTest
private class testClass
{
    static testMethod void test()
    {
        Integer loopCounter;
        
        Test.StartTest();
    
        IPMO_Project_Report ipmo = new IPMO_Project_Report ();
        ipmo.FindAllName();
        ipmo.doSearch();

        Test.stopTest();
        
    }
}

Regards