-1

How to write test class for dependent picklist apex code??

public String Country {get;set;}
    public String state {get;set;}
    public String city {get;set;}

    public List<SelectOption> getCountries()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('None','--- None ---'));
        options.add(new SelectOption('IND','INDIA'));
        options.add(new SelectOption('US','UNITED STATES'));
        return options;
    }
    public List<SelectOption> getStates()
    {
        List<SelectOption> options = new List<SelectOption>();
        if(country == 'IND')
        {
        options.add(new SelectOption('NO','---None---'));
        options.add(new SelectOption('TN','Tamil Nadu'));
        options.add(new SelectOption('KL','Kerala'));
        }
         else if(country == 'US')
         {
        options.add(new SelectOption('NO','---None---'));
        options.add(new SelectOption('TX','TEXAS'));
        options.add(new SelectOption('IL','ILLINOIS'));
        }
        else if  (country == 'None')
        {
        options.add(new SelectOption('None','--- None ---'));
        }
        return options;

    }
     public List<SelectOption> getCities()
    {
        List<SelectOption> options = new List<SelectOption>();
        if(state =='TN')
        {
            options.add(new SelectOption('NO','---None---'));
            options.add(new SelectOption('CHE','Chennai'));
            options.add(new SelectOption('CBE','Coimbatore'));
        }
         else if(state == 'KL')
        {
            options.add(new SelectOption('NO','---None---'));
            options.add(new SelectOption('COA','Coachin'));
            options.add(new SelectOption('ALP','Alleppi'));
        }
         else if(state == 'TX')
        {
            options.add(new SelectOption('NO','---None---'));
            options.add(new SelectOption('HUN','Huston'));
            options.add(new SelectOption('SNA','San Antonio'));
        }
         else if(state == 'IL')
        {
            options.add(new SelectOption('NO','---None---'));
            options.add(new SelectOption('CGO','Chicago'));
            options.add(new SelectOption('ATA','Atlanta'));
        }

        else if  (state == 'NO')
        {
            options.add(new SelectOption('None','--- None ---'));
        }
        return options;
    }
     public String Country1 {get;set;}
     public String state1 {get;set;}
     public String city1 {get;set;}

     public List<SelectOption> getCountries1()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('None','--- None ---'));
        options.add(new SelectOption('IND','INDIA'));
        options.add(new SelectOption('US','UNITED STATES'));
        return options;
    }
    public List<SelectOption> getStates1()
    {
        List<SelectOption> options = new List<SelectOption>();
        if(country1 == 'IND')
        {
        options.add(new SelectOption('NO','---None---'));
        options.add(new SelectOption('TN','Tamil Nadu'));
        options.add(new SelectOption('KL','Kerala'));
        }
         else if(country1 == 'US')
         {
        options.add(new SelectOption('NO','---None---'));
        options.add(new SelectOption('TX','TEXAS'));
        options.add(new SelectOption('IL','ILLINOIS'));
        }
        else if  (country1 == 'None')
        {
        options.add(new SelectOption('None','--- None ---'));
        }
        return options;

    }
     public List<SelectOption> getCities1()
    {
        List<SelectOption> options = new List<SelectOption>();
        if(state1 =='TN')
        {
            options.add(new SelectOption('NO','---None---'));
            options.add(new SelectOption('CHE','Chennai'));
            options.add(new SelectOption('CBE','Coimbatore'));
        }
         else if(state1 == 'KL')
        {
            options.add(new SelectOption('NO','---None---'));
            options.add(new SelectOption('COA','Coachin'));
            options.add(new SelectOption('ALP','Alleppi'));
        }
         else if(state1 == 'TX')
        {
            options.add(new SelectOption('NO','---None---'));
            options.add(new SelectOption('HUN','Huston'));
            options.add(new SelectOption('SNA','San Antonio'));
        }
         else if(state1 == 'IL')
        {
            options.add(new SelectOption('NO','---None---'));
            options.add(new SelectOption('CGO','Chicago'));
            options.add(new SelectOption('ATA','Atlanta'));
        }

        else if  (state1 == 'NO')
        {
            options.add(new SelectOption('None','--- None ---'));
        }
        return options;
    }
  • I am new to salesforce. I am getting confused in writing the test class. – sripuram Dec 22 '14 at 14:28
  • sripuram -- the folks at SFSE love to help but it doesn't look like you've reviewed (or at least demonstrated that you've reviewed) the doc on testing controllers here: salesforce.com/us/developer/docs/pages/… – cropredy Dec 22 '14 at 17:55
2

You need to test it much like you would any other test. You need to set your country to a specific value. Then you check that the states match as they should according to the country that was chosen. So something like this

controller.Country = 'US';

for(SelectOption stateOpt : controller.getStates()){
      system.assertNotEquals('Tamil Nadu', stateOpt.getValue());
      system.assertNotEquals('Kerala', stateOpt.getValue());
}

Obviously, this is just psuedo-code and you will have to adjust it and expand it for some positive tests as well, but this should give you the idea of how to begin thinking about testing it.

  • Can u please explain it clearly. I am new to salesforce. – sripuram Dec 22 '14 at 14:20
0
@isTest
public class TestSample {

    public static testMethod void main(){
         PageReference pageRef = Page.dependentPicklist;
        Test.setCurrentPage(pageRef); 
        sample s = new sample();
        s.getCountries();
        s.getCountries1();

        s.Country = 'US';
        for(SelectOption stateOpt : s.getStates()){
              system.assertNotEquals('Tamil Nadu', stateOpt.getValue());
              system.assertNotEquals('Kerala', stateOpt.getValue());
        }
        s.Country = 'IND';

        for(SelectOption stateOpt : s.getStates()){
              system.assertNotEquals('Tamil Nadu', stateOpt.getValue());
              system.assertNotEquals('Kerala', stateOpt.getValue());
        }
        s.Country = 'None';

        for(SelectOption stateOpt : s.getStates()){
              system.assertNotEquals('Tamil Nadu', stateOpt.getValue());
              system.assertNotEquals('Kerala', stateOpt.getValue());
        }
        s.Country1 = 'US';

        for(SelectOption stateOpt : s.getStates1()){
              system.assertNotEquals('Tamil Nadu', stateOpt.getValue());
              system.assertNotEquals('Kerala', stateOpt.getValue());
        }
        s.Country1 = 'IND';

        for(SelectOption stateOpt : s.getStates1()){
              system.assertNotEquals('Tamil Nadu', stateOpt.getValue());
              system.assertNotEquals('Kerala', stateOpt.getValue());
        }
        s.Country1 = 'None';

        for(SelectOption stateOpt : s.getStates1()){
              system.assertNotEquals('Tamil Nadu', stateOpt.getValue());
              system.assertNotEquals('Kerala', stateOpt.getValue());
        }
        s.state = 'NO';
        for(SelectOption cityOpt : s.getCities()){
              system.assertNotEquals('NO', cityOpt.getValue());               
        }
        s.state1 = 'NO';
        for(SelectOption cityOpt1 : s.getCities1()){
              system.assertNotEquals('NO', cityOpt1.getValue());               
        }
        s.state = 'TN';
        for(SelectOption cityOpt : s.getCities()){             
              system.assertNotEquals('COA', cityOpt.getValue());

        }
        s.state1 = 'TN';
        for(SelectOption cityOpt1 : s.getCities1()){             
              system.assertNotEquals('COA', cityOpt1.getValue());

        }
        s.state = 'KL';
        for(SelectOption cityOpt : s.getCities()){

              system.assertNotEquals('CBE', cityOpt.getValue());

        }
        s.state = 'TX';
        for(SelectOption cityOpt : s.getCities()){

              system.assertNotEquals('ALP', cityOpt.getValue());

        }
        s.state = 'IL';
        for(SelectOption cityOpt : s.getCities()){

              system.assertNotEquals('HUN', cityOpt.getValue());

        }
        s.state1 = 'KL';
        for(SelectOption cityOpt1 : s.getCities1()){

              system.assertNotEquals('CBE', cityOpt1.getValue());

        }
        s.state1 = 'TX';
        for(SelectOption cityOpt1 : s.getCities1()){

              system.assertNotEquals('ALP', cityOpt1.getValue());

        }
        s.state1 = 'IL';
        for(SelectOption cityOpt1 : s.getCities1()){

              system.assertNotEquals('HUN', cityOpt1.getValue());

        }

    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.