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
Controller;
/**
* 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;
//constructor
public desiredLocationsWrapper(){
LstWrp = new List<wrapperLocationCheckbox>();
TleadId = ApexPages.currentPage().getParameters().get('what_id');
String s;
try{
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){
this.checkbox=c;
this.SLNo=s;
this.loc=l;
}
*/
//Another constructor with parameter
public wrapperLocationCheckbox(Location__c l){
this.loc=l;
checkbox=false;
}
}
//---------------------------------------------------------------
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){
if(wrp.checkbox==true){
LstWrp_Selected.add(wrp);
}
}
//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];
if(!existingLocations.isEmpty()){
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'));
pr.setRedirect(true);
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');
pr.setRedirect(false);
return pr;
}*/
//--------------------------------------------------------------------
}
Below is My approach in test class, I get only 59% coverage and some parts are confusing to me;
@isTest(SeeAllData=true)
public class desiredLocationsWrapperTest {
public static desiredLocationsWrapper instance = new desiredLocationsWrapper();
@isTest(SeeAllData=true)
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;
}
@isTest(SeeAllData=true)
public static void saveTest(){
DesiredLocation__c dloc1 = new DesiredLocation__c(Name = 'daw', Area__c = 'a0H0D0000034IMx', Tlead__c = 'a030D000003LUyh' );
insert dloc1;
instance.save();
}
@isTest
public static void cancelTC01(){
instance.cancel();
}
}
LstWrp.add(new wrapperLocationCheckbox(locList[i]));