function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
deeksharth srivastavadeeksharth srivastava 

Help me to write a test class for this wrapper class

public  class PropertyRelatedController 
{
//  declaration Start
   public  List<String> str1 ;
   public List<SelectOption> countrieLst {get;set;}
   String newSetStr ;
   String Selectedstr;
   public  string ContactIds{get;set;}
  // public  string ContactIds1{get;set;}
   list<Contact> con =new list<Contact>();
   list<CampaignMember> cmem = new list<CampaignMember>();
   public TTL_Residential__Property__c cp{get;set;}
   Public List<WrapperClassEx> WrapperList{get;set;}
   public  list<string> ContactId = new list<string>();
   public  list<string> ContactId1 = new list<string>();
   public  list<string> ContactId2 = new list<string>();
   Set<Id> accountId = new Set<ID>();
   
   public string selectedValue{get;set;}
   
 // Controller start from here           
   public PropertyRelatedController(ApexPages.StandardSetController standardController) 
   {
            for(Sobject standcont:standardController.getSelected())
          {
               accountId.add(standcont.id);
          } 
           cp = new TTL_Residential__Property__c();
           selectedValue='';
            system.debug('chechk'+Selectedstr);
          // ContactIds = new list<string>();
           ContactId.clear();  
   }
     public void getContactNames()
    { 
     Selectedstr = selectedValue;
 
   getwrapperObj();
    }
      public List<SelectOption> getItems()
     {
        List<SelectOption> options = new List<SelectOption>();

        options.add(new SelectOption('All','--All--'));
        options.add(new SelectOption('Primary Owner or Owner','Primary Owner and Owner '));
        options.add(new SelectOption('Primary Owner','Primary Owner'));
        options.add(new SelectOption('Owner','Owner'));

        return options;
     }

 
// Wrapper Class Starts from Here 
    
   Public List<WrapperClassEx> getwrapperObj()
   {
   WrapperList = New List<WrapperClassEx>();
   map<string,TTL_Residential__Property__c> mapofproperty = new map<string,TTL_Residential__Property__c>();
    For( TTL_Residential__Property__c propobject : [ select id,name from TTL_Residential__Property__c where id in:accountId ])
     {
             mapofproperty.put(propobject.id,propobject);
      }
      set<string> setofid = new set<string>();
      map<string,WrapperClassEx> mapofwrapper = new map<string,WrapperClassEx>();
      
     if(Selectedstr != null && Selectedstr!='Primary Owner or Owner' && Selectedstr!='All' )
     {
       ContactId.clear();
    
       system.debug('cid'+ContactId);
     for(TTL_Core__Contact_Role__c contactrole : [select id,TTL_Core__Contact__c,TTL_Core__Contact_Role__c,TTL_Commercial__Property__c,TTL_Core__Contact__r.Account.Name from TTL_Core__Contact_Role__c where TTL_Commercial__Property__c in:mapofproperty.keyset() and TTL_Core__Contact_Role__c =: Selectedstr])
    {
           ContactId.add(contactrole.TTL_Core__Contact__c);
            system.debug('cid2'+ContactId);
          
        if(!setofid.contains(contactrole.TTL_Commercial__Property__c))
        {
            setofid.add(contactrole.TTL_Commercial__Property__c);
            mapofwrapper.put(mapofproperty.get(contactrole.TTL_Commercial__Property__c ).id,New WrapperClassEx(mapofproperty.get(contactrole.TTL_Commercial__Property__c ).name,mapofproperty.get(contactrole.TTL_Commercial__Property__c ).id ));
            mapofwrapper.get(contactrole.TTL_Commercial__Property__c).listofcontactrole.add(contactrole);
            WrapperList.add(New WrapperClassEx(mapofproperty.get(contactrole.TTL_Commercial__Property__c ).name,mapofproperty.get(contactrole.TTL_Commercial__Property__c ).id ));
        }
        else
        {
            mapofwrapper.get(contactrole.TTL_Commercial__Property__c).listofcontactrole.add(contactrole);
        }
    }   
    }
       
 
   Public Class WrapperClassEx
   {
     public list<TTL_Core__Contact_Role__c > listofcontactrole{get;set;}
     Public string propertyname{get;set;}
     Public string propertyid{get;set;}
   
     Public WrapperClassEx(string JCity,string propertyid){        
        this.propertyid = propertyid;
        propertyname=JCity;    
        listofcontactrole =  new   list<TTL_Core__Contact_Role__c >();          
     }
   } 

  
  

 }
AshlekhAshlekh
Hi,

I've not written all code for you but give you a idea. Please change the data according to your controller.
 
​@isTest
private class PropertyRelatedControllerTest{
 static testMethod void testBasic() {
    
        Test.startTest();
        PageReference pageRef = Page.pageName;
        Test.setCurrentPageReference(pageRef);
        TTL_Residential__Property__c acc = new TTL_Residential__Property__c(name ='bob');
        insert acc;
        
        //Insert Contact becuase your code need. 
        //
        
        //Insert
        //TTL_Core__Contact_Role__c x = new TTL_Core__Contact_Role__c(name= '', role='');
        //insert x;
        
        ApexPages.CurrentPage().getparameters().put('id', acc.id);
        ApexPages.StandardController sc = new ApexPages.standardController(acc);
        PropertyRelatedController cont = new PropertyRelatedController(sc);
        cont.selectedValue= 'Primary Owner or Owner';
        cont.getContactNames();
        
        //Use some assert function here   

        test.stopTest();
 }

}


-Thanks
Ashlekh Gera