I am trying to write a test class for a Wrapper class. The wrapper class pulls one objects to make it as a checkbox list.

Could anyone help me to adjust the code I wrote below? I have written test classes before but have never tried with wrapper class


 * In addition, this class provides methods for displaying sObject Location__c
 * as a selectoption in checkboxes and latter to be included as an insert

 * @author:  Adnan Zahry
 * @Last Update: 05/08/2019

public class desiredLocationsWrapper{

    //this variable handles location list for which appear on visualforce as checkbox option
    public List<wrapperLocationCheckbox> LstWrp{get;set;}

    //this variable handles selected location and stored back into controller
    public List<wrapperLocationCheckbox> LstWrp_Selected;

    private Id TleadId;

    public desiredLocationsWrapper(){
        LstWrp = new List<wrapperLocationCheckbox>();       
        TleadId = ApexPages.currentPage().getParameters().get('what_id');

        String s; 
            s = [SELECT Country__c from TLead__c where Country__c != null and Id = :TleadId ].Country__c;
        catch(Exception ex){
            Apexpages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.FATAL, 'TLead record does not have any country associated with'));

        List<Location__c> locList = new List<Location__c>([SELECT City__c, Id, Region__c FROM Location__c where Country__c = :s ORDER BY City__c asc]);

        for(Integer i=0; i<locList.size(); i++){
            LstWrp.add(new wrapperLocationCheckbox(locList[i]));

    //define the class for which we want to use the checkbox
        public class wrapperLocationCheckbox{

            public boolean checkbox{get;set;}
            public Location__c loc{get;set;}

        /*//Constructor with no arg
        public wrapperLocationCheckbox(){


        //Constructor with parameter
        public wrapperLocationCheckbox(boolean c, String S, Location__c l){


        //Another constructor with parameter
        public wrapperLocationCheckbox(Location__c l){

        public PageReference save(){

        List<Location__c> existingLocations = new List<Location__c>();
        LstWrp_Selected = new List<wrapperLocationCheckbox>();
        //iterate LstWrp collection and pass the values back into controller and assing them to LstWrp_Selected
        for(wrapperLocationCheckbox wrp : LstWrp){


        //Iterate LstWrp_Selected 
        for(wrapperLocationCheckbox wrp1 : LstWrp_Selected){ 

            DesiredLocation__c dloc = new DesiredLocation__c();

            /// for the new selected location, get the id from the object
            existingLocations = [SELECT Id, Name from Location__c WHERE Name =: wrp1.loc.City__c];

                dloc.TLead__c = ApexPages.currentPage().getParameters().get('what_id');
                dloc.Area__c = wrp1.loc.Id;
                dloc.Name = dloc.TLead__c + ' - ' + dloc.Area__c;
                insert dloc;


            PageReference pr = new PageReference('/'+ApexPages.currentPage().getParameters().get('what_id'));
                return pr;
    public PageReference cancel(){
        PageReference pr = new PageReference('/'+ApexPages.currentPage().getParameters().get('what_id'));

        return pr;

    //Return TLead list in search result form
            /*String keyword;
            List<TLead__c> tleadlist;

            public String getkeyword(){
                return keyword;

            public void setkeyword(String s){
                keyword = s;

            public List<TLead__c> gettleadlist(){
                return tleadlist;

            public PageReference showResults(){
                List<List<TLead__c>> searchList = [FIND :keyword RETURNING TLead__c(Name, Id)] ;
                tleadList = (TLead__c[])searchList[0];
                PageReference pr = new PageReference('/apex/incDLocationsTleadLookup');
                return pr;

Below is My approach in test class, I get only 59% coverage and some parts are confusing to me; enter image description here

enter image description here

public class desiredLocationsWrapperTest {
    public static desiredLocationsWrapper instance = new desiredLocationsWrapper();

    public static void dlocWrapper(){
        Tlead__c tl = new Tlead__c(
          Name = 'Test Adnan1',
          Country__c = 'ID',
          Lead_Source__c = 'Facebook Lead Ad',
          Mobile__c = '81380577103');
        insert tl;

        Location__c lc = new Location__c(
          Name = 'Ciomas',
          Country__c = 'ID',
          City__c = 'Bogor',
          Region__c = 'Jawa Barat');
        insert lc;
    public static void saveTest(){

        DesiredLocation__c dloc1 = new DesiredLocation__c(Name = 'daw', Area__c = 'a0H0D0000034IMx', Tlead__c = 'a030D000003LUyh' );
        insert dloc1;
    public static void cancelTC01(){
  • are you facing any issue here? Please specify – rahul gawale Aug 7 '19 at 8:28
  • Hi Rahul, I have edited my code and added some attachments hope this makes more clear, Im having problem to simulate in test class for this code line LstWrp.add(new wrapperLocationCheckbox(locList[i])); – Adnan Zahry Aug 7 '19 at 11:01

Add below things to your test methods

You need to set the current page for the test.

PageReference myVfPage = Page.YourPageName;

Then you need to add the URL parameter like below. First, query the country record and pass its value


Always try to avoid (SeeAllData=true), you delete it keeping @isTest only as it's not required. The reason you can find here.

Also, it's advised to go through the apex testing documentation.

| improve this answer | |
  • Hi Rahul, LstWrp.add(new wrapperLocationCheckbox(locList[i])); here I create object from wrapper/inner class then pass the values to LstWrp , this is the thing that still confuse me, how I put this in my test class, the wrapper class looks like this public class wrapperLocationCheckbox{ public boolean checkbox{get;set;} public Location__c loc{get;set;} public wrapperLocationCheckbox(Location__c l){ this.loc=l; checkbox=false; } } – Adnan Zahry Aug 9 '19 at 4:47
  • If you set what_id and create proper data it will be automatically called from the constructor desiredLocationsWrapper . Above mentioned code sets current page as YourPageName and then sets countryObj.Id as a URL parameter for the page. – rahul gawale Aug 9 '19 at 6:46
  • Thank you Rahul! – Adnan Zahry Aug 9 '19 at 9:25

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.