7

I have a VisualForce page which uses a controller. Within the controller I have a couple of wrapper classes which hold various form elements on the page. Everything works great, but now it is time to write some test code for it...

I am not able to access the wrapper class from my test class. It errors out saying Invalid type: someWrapper. The wrapper is a public class but I don't think that I have properly instantiated the wrapper class within the controller class.

Here is a general look at what my controller looks like:

public with sharing class someController

    @TestVisible private List<someWrapper> wrapperList {get; set;}

    public class someWrapper {

        public Opportunity  someOpportunity {get; set;}
        public Boolean      someBoolean     {get; set;}

        public someWrapper(Opportunity opp){

            someOpportunity = opp;
            someBoolean = true;

        }

    }

}

And here is my test class. It errors out when I am trying to for loop through the wrapperList list.

someController controller = new someController();

for(someWrapper thisListItem : controller.wrapperList){

    // Do stuff

}

I realize that I must have to instantiate the wrapper class somehow, but I can't figure out how to do it properly.

I have tried the following:

controller.someWrapper wrapperForUseInTest = new controller.someWrapper();

someWrapper wrapperForUseInTest = new controller.someWrapper();

class wrapperForUseInTest = new controller.someWrapper()

Any help would be greatly appreciated!

1 Answer 1

9

Although my wrapper class was defined as public in my controller, I still needed to specify @TestVisible on the controller. Once I did so, it became visible to my Test class.

public with sharing class SomeController
{
  @TestVisible private List<SomeWrapper> wrapperList { get; set; }
  @TestVisible public class SomeWrapper 
  {
    public Opportunity someOpportunity { get; set; }
    public Boolean someBoolean { get; set; }
    
    public SomeWrapper (Opportunity opp) {
      someOpportunity = opp;
      someBoolean = true;
    }
  }
}

I also needed to reference the wrapper as a method of my controller. So my for loop had to be modified, like so:

SomeController controller = new SomeController();

for (SomeController.SomeWrapper thisListItem :controller.wrapperList) {
  // Do stuff
}
4
  • 3
    n.b. @testVisible is a V28 (summer 13) feature; for previous Versions, you could simply specify in your testclass `someController.someWrapper'
    – cropredy
    Jun 18, 2013 at 23:47
  • Thanks for the added note. To confirm this, I changed to 27.0 and removed the @TestVisible.
    – VictorKilo
    Jun 19, 2013 at 18:39
  • Can you instantiate a list of wrappers in the same way? I'm having trouble getting this to work in my test class -> List<someExtension.someWrapper> localList; Aug 26, 2013 at 0:28
  • You should be able to use someWrapper as a list type, just make sure that someExtension is the name of the actual class in which the wrapper resides, NOT the variable name used to hold the instantiated class. I haven't had a chance to test this, but in this case you would use: List<someController.someWrapper> localList;
    – VictorKilo
    Aug 26, 2013 at 17:14

You must log in to answer this question.

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