0

I have a VF page where I am appending labels/values to a dropdown input from Custom Labels, like this:

<apex:selectList label="{!$Label.Type}" value="{!lstInputs.typeValue}" multiselect="false" size="1">
   <apex:selectOption itemValue="{!$Label.Option1}" itemLabel="{!$Label.Label1}"/>
   <apex:selectOption itemValue="{!$Label.Option2}" itemLabel="{!$Label.Label2}"/>
</apex:selectList>
<apex:commandButton value="{!$Label.Submit}"  action="{!callImplementor}" reRender="refresh"/>

I am using a Wrapper class to access all variables from my VF page. In my actual controller, I am using a method that gets this wrapper record as a parameter and builds a HTTP request.

public pagereference callImplementor() {
  try {
        implementor(lstInputs);
  } catch (Exception ex){

  }
}
public HttpRequest implementor(InputWrapper inp) {
    String selectedValue = (inp.typeValue != null ? inp.typeValue : '');
    System.debug('selectedValue :' +selectedValue);
    <--some code -->
}
//Wrapper
public class InputWrapper {
   public string typeValue {get; set;}
   //Other variables. 
}

Test class:

public static String adjValue = 'Option1';
  //test method
  static testmethod void testForCalloutSuccess(){
    Pagereference testPage = Page.MyVFPage;
    test.setCurrentPageReference(testPage);
    testPage.getParameters().put('acctId', actId);

    Test.startTest();

    MyControllerClass ctrl = new MyControllerClass();
    MyControllerClass.InputWrapper lstInput = new MyControllerClass.InputWrapper();
    lstInput.typeValue = adjValue;

    ctrl.callImplementor();
    //ctrl.implementor(lstInput);
    Test.stopTest(); 
}

I am getting my selectedValue as null in my test class. If I declare a test wrapper record, and call the method implementor(InputWrapper inp) by passing this record, some other field values are getting null as some values are set from constructor. How can I set the test data for this <apex:selectList> dropdown field from VF page?

4
  • where are the custom settings - i see custom labels?
    – cropredy
    Commented Aug 20, 2019 at 21:12
  • My bad, those are custom labels. Will update my post. Commented Aug 20, 2019 at 21:12
  • use edit and show the relevant portions of your testmethod
    – cropredy
    Commented Aug 20, 2019 at 21:14
  • @cropredy Updated with test method. Commented Aug 20, 2019 at 21:22

2 Answers 2

1

Your problem is most likely here:

MyControllerClass ctrl = new MyControllerClass();
MyControllerClass.InputWrapper lstInput = new MyControllerClass.InputWrapper();
lstInput.typeValue = adjValue;

this should be:

MyControllerClass ctrl = new MyControllerClass();
ctrl.lstInput = new MyControllerClass.InputWrapper();
ctrl.lstInput.typeValue = adjValue;

That is, you may have some confusion between static variables in your controller that should be object variables.

6
  • Still the selectedValue is null. I can kinda see what's happening with my issue, but really hard to figure it out. Commented Aug 20, 2019 at 21:43
  • Can I use something like this?: testPage.getParameters().put('{!$Label.Option1}', 'Option 1');. All I need is a way to pass values from VF page to controller class. That way, my wrapper record can hold that data and my test can process further. Commented Aug 20, 2019 at 21:51
  • your testmethod needs to simulate solely value="{!lstInputs.typeValue}" and action="{!callImplementor}". Labels are a red herring. Edit your OP and show where lstInput is defined
    – cropredy
    Commented Aug 20, 2019 at 21:58
  • Actually I dont really need lstInput variable in my test class as am calling this method from ctrl: callImplementor(). I am getting all my other variables, as I am setting up in the ctrl itself. How to simulate those VF input variables that can blend right in my test context? Commented Aug 20, 2019 at 22:04
  • I think I figured it.I will post it as an answer. Thanks much for your help. Commented Aug 20, 2019 at 22:11
0

After diving deep, I was able to simulate test data. Thanks to @cropredy for making me think loud. Just to add to my actual test method from my post,

Test.startTest();

MyControllerClass ctrl = new MyControllerClass();
MyControllerClass.InputWrapper lstInput = new MyControllerClass.InputWrapper();
ctrl.lstInput.typeValue = adjValue; //This is the way I should append(simulate) the test data. 
ctrl.callImplementor();

Test.stopTest(); 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .