public class EmployeeWrapper implements Comparable {
public Employee__c emp = new Employee__c();
// Constructor public EmployeeWrapper(Employee__c empRecord) { emp = empRecord; }
// Compare emp based on the Employee__c Age__c. public Integer compareTo(Object compareTo) { // Cast argument to EmployeeWrapper EmployeeWrapper compareToEmp = (EmployeeWrapper)compareTo;
// The return value of 0 indicates that both elements are equal. Integer returnValue = 0; if (emp.Age__c > compareToEmp.emp.Age__c) { // Set return value to a positive value. returnValue = 1; } else if (emp.Directory_Heading__c < compareToEmp.emp.Age__c) { // Set return value to a negative value. returnValue = -1; }
return returnValue; } } }
Test Class:
private class EmployeeControllerTest { static testMethod void empTest() { Employee__c emp = new Employee__c(Name = 'Test', Age__c = 12); EmployeeController.EmployeeWrapper empW = new EmployeeController.EmployeeWrapper(emp); //Covering inner/wrapper class empW.compareTo(empW); //Covering compareTo() } }
For your reference, you can check the below blog too,
Public class WrapperContactWrapper { Public Contact con{get;set;} Public boolean bool{get;set;} public WrapperContactWrapper(Contact c,boolean bool) { this.con = c; this.bool = bool; } }
Public class ContactTable { public String size { get; set; } //This is Our collection of the class/wrapper objects WrapperContactWrapper Public List<WrapperContactWrapper> wrapperlist; Public Integer noOfRecords{get; set;} // Create a new Map to verify whether the contact is already added in the Map Map <id,Contact> SelectedcontactMap = new Map <id,Contact>(); public boolean display{get;set;} public list<Contact> selectedList {get;set;} // instantiate the StandardSetController from a query locator public ApexPages.StandardSetController Setcon { get { if(Setcon == Null) { Setcon = new ApexPages.StandardSetController(Database.getQueryLocator([Select Name,Accountid,Email,MobilePhone,LeadSource from Contact])); // sets the number of records in each page set setCon.setpagesize(10); noOfRecords = setCon.getResultSize(); } return Setcon; } set; } //Returns a list of wrapper objects for the sObjects in the current page set Public List<WrapperContactWrapper> getContact() { getSelectedContact(); // Initilaize the list to add the selected contact wrapperlist = new List <WrapperContactWrapper>(); for(Contact cc : (List<contact>)Setcon.getRecords()) { if( SelectedcontactMap .ContainsKey(cc.id)) { wrapperlist.add (new WrapperContactWrapper(cc,true)); } else { wrapperlist.add(new WrapperContactWrapper(cc,false)); } } return wrapperlist;} public void getSelectedContact(){ if(wrapperlist!=null) { for(WrapperContactWrapper wr:wrapperlist) { if(wr.bool == true) { SelectedcontactMap.put(wr.con.id,wr.con); // Add the selected contact id in to the SelectedcontactMap. } else { SelectedcontactMap.remove(wr.con.id); // If you uncheck the contact, remove it from the selectedcontactMap } } } }
public void clickMe()
{ display = true; getSelectedContact();
selectedList = SelectedcontactMap.values(); } public integer pageNumber { get { return Setcon.getPageNumber(); } set; } }
Sample Code:
========================
Apex Class:
public class EmployeeController {
public static void assignSequence(List<Employee__c> listEmployee, Decimal startNum) {
Decimal initial = startNum;
List<EmployeeWrapper> listEmployeeWrapper = new List<EmployeeWrapper>();
for(Employee__c emp : listEmployee) {
listEmployeeWrapper.add(new EmployeeWrapper(emp));
}
listEmployeeWrapper.sort();
listEmployee.clear();
for(EmployeeWrapper empW : listEmployeeWrapper) {
listEmployee.add(empW.dirPagination);
}
for(Employee__c emp : listEmployee) {
emp.Sequence_in_Section__c = initial;
initial = initial + 10;
}
update listEmployee;
}
public class EmployeeWrapper implements Comparable {
public Employee__c emp = new Employee__c();
// Constructor
public EmployeeWrapper(Employee__c empRecord) {
emp = empRecord;
}
// Compare emp based on the Employee__c Age__c.
public Integer compareTo(Object compareTo) {
// Cast argument to EmployeeWrapper
EmployeeWrapper compareToEmp = (EmployeeWrapper)compareTo;
// The return value of 0 indicates that both elements are equal.
Integer returnValue = 0;
if (emp.Age__c > compareToEmp.emp.Age__c) {
// Set return value to a positive value.
returnValue = 1;
} else if (emp.Directory_Heading__c < compareToEmp.emp.Age__c) {
// Set return value to a negative value.
returnValue = -1;
}
return returnValue;
}
}
}
Test Class:
private class EmployeeControllerTest {
static testMethod void empTest() {
Employee__c emp = new Employee__c(Name = 'Test', Age__c = 12);
EmployeeController.EmployeeWrapper empW = new EmployeeController.EmployeeWrapper(emp); //Covering inner/wrapper class
empW.compareTo(empW);
//Covering compareTo()
}
}
For your reference, you can check the below blog too,
https://salesforce.stackexchange.com/questions/78116/wrapper-class-with-test-class-with-code-coverage-to-complete
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks.
Public class WrapperContactWrapper
{
Public Contact con{get;set;}
Public boolean bool{get;set;}
public WrapperContactWrapper(Contact c,boolean bool)
{
this.con = c;
this.bool = bool;
}
}
Public class ContactTable
{
public String size { get; set; }
//This is Our collection of the class/wrapper objects WrapperContactWrapper
Public List<WrapperContactWrapper> wrapperlist;
Public Integer noOfRecords{get; set;}
// Create a new Map to verify whether the contact is already added in the Map
Map <id,Contact> SelectedcontactMap = new Map <id,Contact>();
public boolean display{get;set;}
public list<Contact> selectedList {get;set;}
// instantiate the StandardSetController from a query locator
public ApexPages.StandardSetController Setcon
{
get
{
if(Setcon == Null)
{
Setcon = new ApexPages.StandardSetController(Database.getQueryLocator([Select Name,Accountid,Email,MobilePhone,LeadSource from Contact]));
// sets the number of records in each page set
setCon.setpagesize(10);
noOfRecords = setCon.getResultSize();
}
return Setcon;
}
set;
}
//Returns a list of wrapper objects for the sObjects in the current page set
Public List<WrapperContactWrapper> getContact()
{
getSelectedContact();
// Initilaize the list to add the selected contact
wrapperlist = new List <WrapperContactWrapper>();
for(Contact cc : (List<contact>)Setcon.getRecords())
{
if( SelectedcontactMap .ContainsKey(cc.id))
{
wrapperlist.add (new WrapperContactWrapper(cc,true));
}
else
{
wrapperlist.add(new WrapperContactWrapper(cc,false));
}
}
return wrapperlist;}
public void getSelectedContact(){
if(wrapperlist!=null)
{
for(WrapperContactWrapper wr:wrapperlist)
{
if(wr.bool == true)
{
SelectedcontactMap.put(wr.con.id,wr.con); // Add the selected contact id in to the SelectedcontactMap.
}
else
{
SelectedcontactMap.remove(wr.con.id); // If you uncheck the contact, remove it from the selectedcontactMap
}
}
}
}
public void clickMe()
{
display = true;
getSelectedContact();
selectedList = SelectedcontactMap.values();
}
public integer pageNumber
{
get
{
return Setcon.getPageNumber();
}
set;
}
}