+ Start a Discussion
DeveloperDeveloper 

Test class... i covered 51% i need above 75% coverage please help me...

public class sample1
{
    public String state {get;set;}
    public String city {get;set;}
    public String Village {get;set;}
  // public String city {get;set;}

    public List<SelectOption> getStates()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('None','--- None ---'));        
        options.add(new SelectOption('TN','Tamil Nadu'));
        options.add(new SelectOption('KL','Kerala'));
         options.add(new SelectOption('KA','Karnataka'));
          options.add(new SelectOption('AP','Andhrapradesh'));
                 
        return options;
    } 
                     
    public List<SelectOption> getCities()
    
    {
        List<SelectOption> options = new List<SelectOption>();
        if(state == 'TN')
        {       
            options.add(new SelectOption('CHE','Chennai'));
            options.add(new SelectOption('CBE','Coimbatore'));
        }
        else if(state == 'AP')
        {       
            options.add(new SelectOption('KUR','Kurnool'));
            options.add(new SelectOption('KDP','Kadapa'));
        }
        
        else if(state == 'KL')
        {       
            options.add(new SelectOption('COA','Coachin'));
            options.add(new SelectOption('MVL','Mavelikara'));
        }
        
        
        else if(state == 'KA')
        {       
            options.add(new SelectOption('BAN','Bangalore'));
            options.add(new SelectOption('My','Mysure'));
        }
        
        else
        {
            options.add(new SelectOption('None','--- None ---'));
        }      
        return options;
    }       
 //===============================================================================   
   
      public List<SelectOption> getvillages()
    {
            
   system.debug('city '+city );
    List<SelectOption> options = new List<SelectOption>();
          
          
        // options.add(new SelectOption('None','--- None ---')); 
          //List<SelectOption> options = new List<SelectOption>();
          
        if(City == 'KUR')
        {       
            options.add(new SelectOption('AL','ALur'));
            options.add(new SelectOption('AD','Adoni'));
        }
          
           else if(City == 'KDP')
        {       
            options.add(new SelectOption('A','a1'));
            options.add(new SelectOption('B','b1'));
        }
        
         else if(City == 'CHE')
        {       
            options.add(new SelectOption('c','c1'));
            options.add(new SelectOption('d','d1'));
        }
        
         else if(City == 'CBE')
        {       
            options.add(new SelectOption('e','e1'));
            options.add(new SelectOption('f','f1'));
        }
        
         else if(City == 'COA')
        {       
            options.add(new SelectOption('g','g1'));
            options.add(new SelectOption('h','h1'));
        }
        
         else if(City == 'MVL')
        {       
            options.add(new SelectOption('i','i1'));
            options.add(new SelectOption('j','j1'));
        }
        
         else if(City == 'BAN')
        {       
            options.add(new SelectOption('k','k1'));
            options.add(new SelectOption('l','l1'));
        }
        
         else if(City == 'My')
        {       
            options.add(new SelectOption('m','m1'));
            options.add(new SelectOption('n','n1'));
        }
        
          else
        {
            options.add(new SelectOption('None','--- None ---'));
        } 
          
     return options;  
    } 
}
===================================================

@isTest
private class sample1_Test{
   static testMethod void test_getStates_UseCase1(){
    sample1 obj01 = new sample1();
    obj01.state = 'test data';
    obj01.city = 'test data';
    obj01.Village = 'test data';
    obj01.getStates();
  }
   static testMethod void test_getCities_UseCase1(){
    sample1 obj01 = new sample1();
    obj01.state = 'test data';
    obj01.city = 'test data';
    obj01.Village = 'test data';
    obj01.getCities();
  }
   static testMethod void test_getvillages_UseCase1(){
    sample1 obj01 = new sample1();
    obj01.state = 'test data';
    obj01.city = 'test data';
    obj01.Village = 'test data';
    obj01.getvillages();
  }
}

Thanks In advance.......

Regards 
Gopal M 
Best Answer chosen by Developer
DeveloperDeveloper
@isTest
public class sample1_test {
@isTest static void testgetStates()
   {
       Test.startTest();
       sample1 sam = new sample1();
       sam.Village='Koderma';
       sam.getStates();
       Test.stopTest();
   }
   @isTest static void testgetCities1()
   {
       Test.startTest();
       sample1 sam = new sample1();
       sam.state='TN';
       sam.getCities();
       Test.stopTest();
   }
   @isTest static void testgetCities2()
   {
       Test.startTest();
       sample1 sam = new sample1();
       sam.state='AP';
       sam.getCities();
       Test.stopTest();
   }
   @isTest static void testgetCities3()
   {
       Test.startTest();
       sample1 sam = new sample1();
       sam.state='KL';
       sam.getCities();
       Test.stopTest();
   }
   @isTest static void testgetCities4()
   {
       Test.startTest();
       sample1 sam = new sample1();
       sam.state='KA';
       sam.getCities();
       Test.stopTest();
   }
   @isTest static void testgetCities5()
   {
       Test.startTest();
       sample1 sam = new sample1();
       sam.state='AA';
       sam.getCities();
       Test.stopTest();
   }
   @isTest static void testgetvillages1()
   {
       Test.startTest();
       sample1 sam = new sample1();
       sam.City='KUR';
       sam.getvillages();
       Test.stopTest();
   }
   @isTest static void testgetvillages2()
   {
       Test.startTest();
       sample1 sam = new sample1();
       sam.City='KDP';
       sam.getvillages();
       Test.stopTest();
   }
   @isTest static void testgetvillages3()
   {
       Test.startTest();
       sample1 sam = new sample1();
       sam.City='CHE';
       sam.getvillages();
       Test.stopTest();
   }
   @isTest static void testgetvillages4()
   {
       Test.startTest();
       sample1 sam = new sample1();
       sam.City='CBE';
       sam.getvillages();
       Test.stopTest();
   }
   @isTest static void testgetvillages5()
   {
       Test.startTest();
       sample1 sam = new sample1();
       sam.City='COA';
       sam.getvillages();
       Test.stopTest();
   }
   @isTest static void testgetvillages6()
   {
       Test.startTest();
       sample1 sam = new sample1();
       sam.City='MVL';
       sam.getvillages();
       Test.stopTest();
   }
   @isTest static void testgetvillages7()
   {
       Test.startTest();
       sample1 sam = new sample1();
       sam.City='BAN';
       sam.getvillages();
       Test.stopTest();
   }
   @isTest static void testgetvillages8()
   {
       Test.startTest();
       sample1 sam = new sample1();
       sam.City='My';
       sam.getvillages();
       Test.stopTest();
   }
   @isTest static void testgetvillages9()
   {
       Test.startTest();
       sample1 sam = new sample1();
       sam.City='AAA';
       sam.getvillages();
       Test.stopTest();
   }
}

 

All Answers

Prakash NawalePrakash Nawale
Hi Gopan,

you need to set values for city and statege variables 

obj01.state = 'KA'; 
obj01.getStates();
obj01.state = 'BAN'; 
obj01.getStates();
.....
.....
Do for all to meet conditions and get code coverage.
you can achive this with adding stages in list then iterate over list and set stage value and call getStages() to do with few lines of code.
 
DeveloperDeveloper
@isTest
public class sample1_test {
@isTest static void testgetStates()
   {
       Test.startTest();
       sample1 sam = new sample1();
       sam.Village='Koderma';
       sam.getStates();
       Test.stopTest();
   }
   @isTest static void testgetCities1()
   {
       Test.startTest();
       sample1 sam = new sample1();
       sam.state='TN';
       sam.getCities();
       Test.stopTest();
   }
   @isTest static void testgetCities2()
   {
       Test.startTest();
       sample1 sam = new sample1();
       sam.state='AP';
       sam.getCities();
       Test.stopTest();
   }
   @isTest static void testgetCities3()
   {
       Test.startTest();
       sample1 sam = new sample1();
       sam.state='KL';
       sam.getCities();
       Test.stopTest();
   }
   @isTest static void testgetCities4()
   {
       Test.startTest();
       sample1 sam = new sample1();
       sam.state='KA';
       sam.getCities();
       Test.stopTest();
   }
   @isTest static void testgetCities5()
   {
       Test.startTest();
       sample1 sam = new sample1();
       sam.state='AA';
       sam.getCities();
       Test.stopTest();
   }
   @isTest static void testgetvillages1()
   {
       Test.startTest();
       sample1 sam = new sample1();
       sam.City='KUR';
       sam.getvillages();
       Test.stopTest();
   }
   @isTest static void testgetvillages2()
   {
       Test.startTest();
       sample1 sam = new sample1();
       sam.City='KDP';
       sam.getvillages();
       Test.stopTest();
   }
   @isTest static void testgetvillages3()
   {
       Test.startTest();
       sample1 sam = new sample1();
       sam.City='CHE';
       sam.getvillages();
       Test.stopTest();
   }
   @isTest static void testgetvillages4()
   {
       Test.startTest();
       sample1 sam = new sample1();
       sam.City='CBE';
       sam.getvillages();
       Test.stopTest();
   }
   @isTest static void testgetvillages5()
   {
       Test.startTest();
       sample1 sam = new sample1();
       sam.City='COA';
       sam.getvillages();
       Test.stopTest();
   }
   @isTest static void testgetvillages6()
   {
       Test.startTest();
       sample1 sam = new sample1();
       sam.City='MVL';
       sam.getvillages();
       Test.stopTest();
   }
   @isTest static void testgetvillages7()
   {
       Test.startTest();
       sample1 sam = new sample1();
       sam.City='BAN';
       sam.getvillages();
       Test.stopTest();
   }
   @isTest static void testgetvillages8()
   {
       Test.startTest();
       sample1 sam = new sample1();
       sam.City='My';
       sam.getvillages();
       Test.stopTest();
   }
   @isTest static void testgetvillages9()
   {
       Test.startTest();
       sample1 sam = new sample1();
       sam.City='AAA';
       sam.getvillages();
       Test.stopTest();
   }
}

 
This was selected as the best answer