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!

8

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

}
  • 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 '13 at 23:47
  • Thanks for the added note. To confirm this, I changed to 27.0 and removed the @TestVisible. – VictorKilo Jun 19 '13 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; – Matthew Lamb Aug 26 '13 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 '13 at 17:14

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.