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
sudh shuklasudh shukla 

some help me to increase the code coverage of the following class:

Here is my class:
public class dependcustompicklist {

    public String selectcity { get; set; }

    public String selectcountry { get; set; }
    
    public Boolean test {get;set;}
    
    public dependcustompicklist(){
    }
    
    public void testFunction(){
        test = true;
    }
    
    public list <SelectOption> getcountry()
    {
        list <SelectOption> opt = new list <SelectOption> ();
        opt.add(new SelectOption ('','Select'));
        opt.add(new SelectOption ('India','India'));
        opt.add(new SelectOption ('US','US'));
        opt.add(new SelectOption ('UK','UK'));
        return opt;
    }
    
        public list <SelectOption> getcity()
    {
        list <SelectOption> opt1 = new list <SelectOption> ();
        if(selectcountry != null && test != false)
            {
                if(selectcountry == 'India')
                {
                opt1.add(new SelectOption ('','Select'));
                opt1.add(new SelectOption ('Kanpur','Kanpur',true));
                opt1.add(new SelectOption ('Ghaziabad','Ghaziabad'));
                opt1.add(new SelectOption ('Noida','Noida'));
                }
                if(selectcountry == 'US')
                {
                opt1.add(new SelectOption ('','Select'));
                opt1.add(new SelectOption ('Washington DC','Washington DC'));
                opt1.add(new SelectOption ('New York','New York'));
                opt1.add(new SelectOption ('Los Angeles','Los Angeles'));
                }
                if(selectcountry == 'UK')
                {
                opt1.add(new SelectOption ('','Select'));
                opt1.add(new SelectOption ('London','London'));
                opt1.add(new SelectOption ('Paris','Paris'));
                //opt1.add(new SelectOption ('',''));
                }
            }
        return opt1;
    }
}

 
Best Answer chosen by sudh shukla
Shashi PatowaryShashi Patowary
Hi Sudh,

Please find the below testclass with 90% coverage.


@isTest  private class dependcustompicklistTestClass {    
     static testMethod void validateGetcountry() {   
           dependcustompicklist  dep = new dependcustompicklist();     
         dep.getCountry();              
  
        dep.selectcountry = 'India';        
        dep.getcity();        
         dep.selectcountry = 'US';      
        dep.getcity();      
        dep.selectcountry = 'UK';        
       dep.getcity();    
    }
}

Please note that you need put some assert methods as best practices.I just created in a hurry.

Please let me know if it helps.

Thanks,
Shashi
 

All Answers

Shashi PatowaryShashi Patowary
Hi Sudh,

Please find the below testclass with 90% coverage.


@isTest  private class dependcustompicklistTestClass {    
     static testMethod void validateGetcountry() {   
           dependcustompicklist  dep = new dependcustompicklist();     
         dep.getCountry();              
  
        dep.selectcountry = 'India';        
        dep.getcity();        
         dep.selectcountry = 'US';      
        dep.getcity();      
        dep.selectcountry = 'UK';        
       dep.getcity();    
    }
}

Please note that you need put some assert methods as best practices.I just created in a hurry.

Please let me know if it helps.

Thanks,
Shashi
 
This was selected as the best answer
sudh shuklasudh shukla
Thanks Shashi for your reply its work i made some changes and its giving 100% code coverage:
@isTest
  private class dependcustompicklistTestClass {    
     static testMethod void validateGetcountry() {   
           dependcustompicklist  dep = new dependcustompicklist();     
           dep.getCountry();              
           dep.selectcity='';
           dep.selectcountry = 'India';        
           dep.getcity();        
           dep.selectcountry = 'US';      
           dep.getcity();      
           dep.selectcountry = 'UK';        
           dep.getcity();    
           dep.test=true;
           dep.testFunction();
       
    }
}