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
Mikey BooshMikey Boosh 

How to write a test class for a Wrapper class

Hi there,

I need some help writing a test class for the following wrapper class.

Any thoughts? Thanks!
 
public class CustomerStatementTableEmailController {

    public Id recId {get;set;}
    
    public List<Delivery_Note__c> notes {
        get {
            RETURN recId != NULL ? [
            SELECT id,name,Balance__c,Transaction_Amount__c,Transaction_Date__c,Transaction_Number__c,Transaction_Type__c,
                Transaction_Balance__c,ABM_Invoice_Amount__c,ABM_Credit_Note_Amount__c,zTransactionBalanceForStatement__c
            FROM Delivery_Note__c
            WHERE order__r.AccountId = :recId
            AND (Transaction_Balance__c != 0.00 OR Transaction_Date__c = LAST_MONTH)
            ] : new List<Delivery_Note__c>();
        } 
        private set;
    }
    
    public List<Remittance_Advice__c> remittances {
        get {
            RETURN recId != NULL ? [
            SELECT id,Name,Account__r.id,Date__c,Remittance_Total_Credit__c,Remittance_Total_Debit__c
            FROM Remittance_Advice__c
            WHERE Account__r.id = :recId
            AND Date__c = LAST_MONTH
            ] : new List<Remittance_Advice__c>();
        } 
        private set;
    }
    
    public list<Delivery_Note__c> notDue {
        get {
            RETURN recId != NULL ? [
                SELECT id,Invoice_Total_Amount_Including_GST__c,Balance__c,recordType.Name,Transaction_Balance__c FROM Delivery_Note__c
                WHERE zAgeTrialBalance__c = 'Current'
                AND recordType.Name = 'Delivery Note / Invoice'
                AND order__r.Accountid = :recId] : new List<Delivery_Note__c>();
        }
        set;
    }
    
    public list<Delivery_Note__c> notDueC {
        get {
            RETURN recId != NULL ? [
                SELECT id,Credit_Note_Total_Including_GST__c,Balance__c,Transaction_Balance__c FROM Delivery_Note__c
                WHERE zAgeTrialBalanceCredit__c = 'Current'
                AND recordType.Name = 'Credit Note'
                AND order__r.Accountid = :recId] : new List<Delivery_Note__c>();
        }
        set;
    }
    

    public Wrapper[] wrappers {
        get{
            wrappers = new List<Wrapper>(); 
            for (Delivery_Note__c note: notes){
                wrappers.add(new Wrapper(note));
            }
            for (Remittance_Advice__c remittance: remittances){
                wrappers.add(new Wrapper(remittance));
            }
            wrappers.sort();
            return wrappers;
        } 
        private set;
    }


    
    public class Wrapper implements Comparable {
    
        public Delivery_Note__c note {get; private set;}
        public Remittance_Advice__c remittance {get; private set;}
        public String dataType {get;private set;}
        public Date calculatedDate {get;private set;}
        public String calculatedNumber {get;private set;}
        public String calculatedType {get;private set;}
        public Decimal calculatedAmount {get;private set;}
        public Decimal calculatedPaid {get;private set;}
        public Decimal calculatedBalance {get;private set;}
        public Id calculatedId {get;private set;}
        
        public Wrapper(Delivery_Note__c n) {
            note = n;
            dataType = 'note';
            calculatedDate = note.Transaction_Date__c;
            calculatedNumber = note.Transaction_Number__c;
            calculatedAmount = note.Transaction_Amount__c;
            calculatedPaid = 0.00;
            calculatedBalance = note.zTransactionBalanceForStatement__c;
            calculatedType = note.Transaction_Type__c;
            calculatedId = note.id;
        }

        public Wrapper(Remittance_Advice__c r) {
            remittance = r;
            dataType = 'remittance';
            calculatedDate = remittance.Date__c;
            calculatedNumber = remittance.name;
            calculatedAmount = remittance.Remittance_Total_Credit__c;
            calculatedPaid = remittance.Remittance_Total_Credit__c;
            calculatedBalance = 0.00;
            calculatedType = 'Receipt';
            calculatedId = remittance.id;
        }
        
        public Integer compareTo(Object instance){
            Wrapper that = (Wrapper)instance;
            if (this.calculatedDate > that.calculatedDate) return 1;
            if (this.calculatedDate < that.calculatedDate) return -1;
            return 0;
        }
    }
}

 
Deepali KulshresthaDeepali Kulshrestha
Hi Mikey,

A Wrapper class is a class whose instances are a collection of other objects. It is used to display different objects on a Visual Force page in same table

Please refer to the following links as they may assist you in writing the test class for the wrapper class:

https://salesforce.stackexchange.com/questions/87533/writing-test-class-for-wrapper-class
https://salesforce.stackexchange.com/questions/61175/test-class-for-wrapper-class
https://developer.salesforce.com/forums/?id=906F0000000919gIAA
http://www.infallibletechie.com/2014/07/how-to-cover-wrapper-class-in-test.html
https://www.forcetalks.com/salesforce-topic/how-to-cover-test-class-for-wrapper-class-in-salesforce/

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha