You need to sign in to do that
Don't have an account?
nikita dhamal
test code to cover wrapperclass
I have a controller with wrapper class in it..can someone help to to write test class for it??
My controller is:
public class ContactSelectClassController{
public String beforeblob1{get;set;}
public String conid{get;set;}
public String emailEncoded { get; set; }
public String email { get; set; }
public String phoneEncoded { get; set; }
public String con_number { get; set; }
public String fullname{get;set;}
public String emailName{get;set;}
public String fullnameEncoded{get;set;}
@TestVisible public List<wrapAccount> wrapAccountList {get; set;}
public List<Contact> selectedAccounts{get;set;}
public String feedid;
private List<Id> contactids=new list<Id>();
public List<Contact> conts;
public List<Contact> cont;
public List<Feedback_Contacts__c> feedcon{get;set;}
public ContactSelectClassController(ApexPages.StandardController controller)
{
/* conid = ApexPages.currentPage().getParameters().get('conid');
system.debug('+++++++++CONID+++++++++'+conid);
Contact con =[Select firstname,lastname,email,phone,id from contact where id=:conid];
system.debug('+++++++++CON+++++++++'+con ); */
}
public ContactSelectClassController()
{
feedid= (ApexPages.currentPage().getParameters().get('fid'));
emailName= (ApexPages.currentPage().getParameters().get('emailName'));
fullnameEncoded = ApexPages.currentPage().getParameters().get('fullname');
emailEncoded = ApexPages.currentPage().getParameters().get('email');
phoneEncoded = ApexPages.currentPage().getParameters().get('con_number');
system.debug('*********************Feedback id is :*********************************'+fullname+' '+email+' '+con_number+' '+feedid+' '+emailName);
system.debug('*********************contactidscontactidscontactids:*********************************'+selectedAccounts);
if(wrapAccountList == null)
{
wrapAccountList = new List<wrapAccount>();
for(Contact a: [select Id, Name,Email,Phone from Contact])
{
wrapAccountList.add(new wrapAccount(a));
}
}
}
public pageReference CancelAction()
{
PageReference pr1 = new PageReference('/apex/FeedbackMainlist');
pr1.setRedirect(true);
return pr1;
}
public void processSelected()
{
feedid= (ApexPages.currentPage().getParameters().get('id'));
system.debug('+++++++FEEDID++++++++'+feedid);
fullnameEncoded = ApexPages.currentPage().getParameters().get('fullname');
system.debug('++++++++NAMEENCODED++++++'+fullnameEncoded );
if(fullnameEncoded !=null || fullnameEncoded =='')
{
Blob bodyBlob1 =EncodingUtil.base64Decode(fullnameEncoded );
fullname = bodyBlob1.toString();
system.debug('++++++++fullname ++++++'+fullname);
}
else{
fullname = ApexPages.currentPage().getParameters().get('fullname');
}
emailEncoded = ApexPages.currentPage().getParameters().get('email');
if(emailEncoded !=null || emailEncoded =='')
{
Blob bodyBlob1 =EncodingUtil.base64Decode(emailEncoded );
email = bodyBlob1.toString();
}
else{
email = ApexPages.currentPage().getParameters().get('email');
}
phoneEncoded = ApexPages.currentPage().getParameters().get('con_number');
if(phoneEncoded !=null )
{
Blob bodyBlob2 =EncodingUtil.base64Decode(phoneEncoded );
con_number =bodyBlob2.toString();
}
else{
con_number=ApexPages.currentPage().getParameters().get('con_number');
}
selectedAccounts = new List<Contact>();
for(wrapAccount wrapAccountObj : wrapAccountList)
{
if(wrapAccountObj.selected == true)
{
selectedAccounts.add(wrapAccountObj.acc);
}
}
for(Contact cont: selectedAccounts)
{
contactids.add(cont.Id);
}
feedcon=new List<Feedback_Contacts__c>();
for(Contact cont: selectedAccounts)
{
cont = [Select firstname,lastname,email,id,name,MobilePhone from Contact where id in :selectedAccounts ];
system.debug('+++++++++CONT++++++++++'+cont );
string fullname = cont.firstname + ' ' + cont.lastname;
if(fullname !=null && email!=null && con_number!=null )
{
Blob beforeblob = Blob.valueOf(fullname);
EncodingUtil.urlEncode(fullname,'UTF-8');
string email = cont.email;
Blob beforeblob1 = Blob.valueOf(email);
EncodingUtil.urlEncode(email,'UTF-8');
string con_number = cont.MobilePhone;
Blob beforeblob2 = Blob.valueOf(con_number);
EncodingUtil.urlEncode(con_number,'UTF-8');
Feedback_Contacts__c fc=new Feedback_Contacts__c();
fc.FeedbackContactName__c=cont.Id;
fc.FeedBackNo__c=feedid;
fc.test__c = EncodingUtil.base64Encode(beforeblob ) ;
fc.Email__c = EncodingUtil.base64Encode(beforeblob1 ) ;
fc.ConNumber__c= EncodingUtil.base64Encode(beforeblob2 ) ;
feedcon.add(fc);
}
insert feedcon;
}
Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
mail.setTargetObjectIds(contactids);
mail.setTemplateId('00X360000015j6P');
Messaging.sendEmail(new Messaging.MassEmailMessage[] {mail});
}
public class wrapAccount {
public Contact acc {get; set;}
public Boolean selected {get; set;}
public wrapAccount(Contact a) {
acc = a;
selected = false;
}
}
}
My controller is:
public class ContactSelectClassController{
public String beforeblob1{get;set;}
public String conid{get;set;}
public String emailEncoded { get; set; }
public String email { get; set; }
public String phoneEncoded { get; set; }
public String con_number { get; set; }
public String fullname{get;set;}
public String emailName{get;set;}
public String fullnameEncoded{get;set;}
@TestVisible public List<wrapAccount> wrapAccountList {get; set;}
public List<Contact> selectedAccounts{get;set;}
public String feedid;
private List<Id> contactids=new list<Id>();
public List<Contact> conts;
public List<Contact> cont;
public List<Feedback_Contacts__c> feedcon{get;set;}
public ContactSelectClassController(ApexPages.StandardController controller)
{
/* conid = ApexPages.currentPage().getParameters().get('conid');
system.debug('+++++++++CONID+++++++++'+conid);
Contact con =[Select firstname,lastname,email,phone,id from contact where id=:conid];
system.debug('+++++++++CON+++++++++'+con ); */
}
public ContactSelectClassController()
{
feedid= (ApexPages.currentPage().getParameters().get('fid'));
emailName= (ApexPages.currentPage().getParameters().get('emailName'));
fullnameEncoded = ApexPages.currentPage().getParameters().get('fullname');
emailEncoded = ApexPages.currentPage().getParameters().get('email');
phoneEncoded = ApexPages.currentPage().getParameters().get('con_number');
system.debug('*********************Feedback id is :*********************************'+fullname+' '+email+' '+con_number+' '+feedid+' '+emailName);
system.debug('*********************contactidscontactidscontactids:*********************************'+selectedAccounts);
if(wrapAccountList == null)
{
wrapAccountList = new List<wrapAccount>();
for(Contact a: [select Id, Name,Email,Phone from Contact])
{
wrapAccountList.add(new wrapAccount(a));
}
}
}
public pageReference CancelAction()
{
PageReference pr1 = new PageReference('/apex/FeedbackMainlist');
pr1.setRedirect(true);
return pr1;
}
public void processSelected()
{
feedid= (ApexPages.currentPage().getParameters().get('id'));
system.debug('+++++++FEEDID++++++++'+feedid);
fullnameEncoded = ApexPages.currentPage().getParameters().get('fullname');
system.debug('++++++++NAMEENCODED++++++'+fullnameEncoded );
if(fullnameEncoded !=null || fullnameEncoded =='')
{
Blob bodyBlob1 =EncodingUtil.base64Decode(fullnameEncoded );
fullname = bodyBlob1.toString();
system.debug('++++++++fullname ++++++'+fullname);
}
else{
fullname = ApexPages.currentPage().getParameters().get('fullname');
}
emailEncoded = ApexPages.currentPage().getParameters().get('email');
if(emailEncoded !=null || emailEncoded =='')
{
Blob bodyBlob1 =EncodingUtil.base64Decode(emailEncoded );
email = bodyBlob1.toString();
}
else{
email = ApexPages.currentPage().getParameters().get('email');
}
phoneEncoded = ApexPages.currentPage().getParameters().get('con_number');
if(phoneEncoded !=null )
{
Blob bodyBlob2 =EncodingUtil.base64Decode(phoneEncoded );
con_number =bodyBlob2.toString();
}
else{
con_number=ApexPages.currentPage().getParameters().get('con_number');
}
selectedAccounts = new List<Contact>();
for(wrapAccount wrapAccountObj : wrapAccountList)
{
if(wrapAccountObj.selected == true)
{
selectedAccounts.add(wrapAccountObj.acc);
}
}
for(Contact cont: selectedAccounts)
{
contactids.add(cont.Id);
}
feedcon=new List<Feedback_Contacts__c>();
for(Contact cont: selectedAccounts)
{
cont = [Select firstname,lastname,email,id,name,MobilePhone from Contact where id in :selectedAccounts ];
system.debug('+++++++++CONT++++++++++'+cont );
string fullname = cont.firstname + ' ' + cont.lastname;
if(fullname !=null && email!=null && con_number!=null )
{
Blob beforeblob = Blob.valueOf(fullname);
EncodingUtil.urlEncode(fullname,'UTF-8');
string email = cont.email;
Blob beforeblob1 = Blob.valueOf(email);
EncodingUtil.urlEncode(email,'UTF-8');
string con_number = cont.MobilePhone;
Blob beforeblob2 = Blob.valueOf(con_number);
EncodingUtil.urlEncode(con_number,'UTF-8');
Feedback_Contacts__c fc=new Feedback_Contacts__c();
fc.FeedbackContactName__c=cont.Id;
fc.FeedBackNo__c=feedid;
fc.test__c = EncodingUtil.base64Encode(beforeblob ) ;
fc.Email__c = EncodingUtil.base64Encode(beforeblob1 ) ;
fc.ConNumber__c= EncodingUtil.base64Encode(beforeblob2 ) ;
feedcon.add(fc);
}
insert feedcon;
}
Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
mail.setTargetObjectIds(contactids);
mail.setTemplateId('00X360000015j6P');
Messaging.sendEmail(new Messaging.MassEmailMessage[] {mail});
}
public class wrapAccount {
public Contact acc {get; set;}
public Boolean selected {get; set;}
public wrapAccount(Contact a) {
acc = a;
selected = false;
}
}
}
To cover the code coverage of wrapper class, you need to look into the following links.
http://salesforce.stackexchange.com/questions/12959/how-do-you-access-a-wrapper-class-from-a-test-class
http://www.infallibletechie.com/2014/07/how-to-cover-wrapper-class-in-test.html
If this answer solves your query, please mark it as the 'Best Answer' so others may find it useful.