0

I am trying to write a test class for a Comparable Wrapper with little success. The wrapper class pulls in two objects to make one single list.

I have two issues...

  • I don't know how to cover the code within the wrapper class. I am struggling with the syntax and or finding a similar example
  • I don't know how to cover the get/set lists properly (see screenshot)

get/set

I have looked at other posts, and cant make sense of them relating to my attempts, any help would be appreciated!

APEX:

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;
        }
    }
}

My attempt at the test class:

@isTest
public class TestCustStatementTableEmailController {
    public static testMethod void CustomerStatementTableController() {

        Id accRTId = Account.SObjectType.getDescribe().getRecordTypeInfosByName().get('Customer').getRecordTypeId();
        Id delNoteDelNoteRTId = Delivery_Note__c.SObjectType.getDescribe().getRecordTypeInfosByName().get('Delivery Note / Invoice').getRecordTypeId();
        Id delNoteCreditNoteRTId = Delivery_Note__c.SObjectType.getDescribe().getRecordTypeInfosByName().get('Credit Note').getRecordTypeId();
        Id ordRTId = Order.SObjectType.getDescribe().getRecordTypeInfosByName().get('Sales Order').getRecordTypeId();

        Account newAcc = new Account(recordTypeId=accRTId,name='Test Account',Preferred_Trading_Terms__c='7 Day Term',Preferred_Delivery_Method__c='Local');
        insert newAcc;

        Order newOrd = new Order(recordTypeId = ordRTId,name = 'Test Order',AccountId = newAcc.id,Trading_Terms__c = '7 Day Term',Type = 'both',
            Status = 'Draft',Delivery_Method__c = 'Local',EffectiveDate = date.TODAY(),Stage__c = 'Open Order');
        insert newOrd;

        Delivery_Note__c newDelNote1 = new Delivery_Note__c(recordTypeid = delNoteDelNoteRTId,
            order__c = newOrd.id,Invoice_Date__c = date.today());insert newDelNote1;
        Delivery_Note__c newDelNote2 = new Delivery_Note__c(recordTypeid = delNoteCreditNoteRTId,
            order__c = newOrd.id,Invoice_Date__c = date.today());insert newDelNote2;

        Bank_statement__c newBank = new Bank_Statement__c();insert newBank;

        Remittance_Advice__c newRem = new Remittance_Advice__c(account__c = newAcc.id,Bank_Statement__c = newBank.id);
        insert newRem;

        PageReference pageRef = Page.CustomerStatementPrint;
        pageRef.getParameters().put('id', newAcc.Id );
        Test.setCurrentPage(pageRef);

        CustomerStatementTableEmailController wrap = new CustomerStatementTableEmailController();

        wrap.recId = newAcc.Id;
        wrap.notDue = [
                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 = :newAcc.Id];
        wrap.notDueC = [
                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 = 'Cedit Note'
                AND order__r.Accountid = :newAcc.Id];

        for (CustomerStatementTableEmailController.Wrapper wp :wrap.wrappers) {


        //    wp.calculatedAmount = 0.00;
        //    wp.calculatedBalance = 0.00;
        //    wp.calculatedDate = date.today();
        //    wp.calculatedId = newAcc.Id;
        }
    }
}

1 Answer 1

3

There is nothing inherently special about a wrapper class vis-a-vis unit testing. Like other Apex code, you need to implement the three steps of all core unit testing:

  1. Design test data.
  2. Execute code within unit test context.
  3. Validate results by making assertions.

Here, your wrapper class Wrapper has two constructors, accepting different sObjects, and implements Comparable. This suggests to me a small number of unit tests you would need to write:

  • Validate that behavior is correct when initializing with Remittance_Advice__c.
  • Validate that behavior is correct when initializing with Delivery_Note__c.
  • Validate that different collections (List<Wrapper>) of Wrapper objects are correctly sorted. Because your wrapper sorting is pretty simple and relies on the data population in the constructors to calculate a comparison date, your sorting test can also be pretty simple - just populate a List with several wrappers that are not in order, sort it, and assert that the results are as expected. Make sure your list contains items that will compare to one another as greater than, less than, and equal so that you can validate all comparison cases and obtain full coverage.

As far as coverage of your main controller goes:

You're explicitly setting notDue and other properties rather than calling the getter method (by accessing the property) and validating the results. This means you get coverage on the set synthesized setter, but not on the getter, where your actual logic lives. Call the getter in test context after creating and initializing a controller, with known test data, by accessing the property. Then, write assertions to show that the returned sObject data is as expected given the data setup.

2
  • Thanks David, I understand the context of your answer, however I struggle with the syntax. For instance, the wrapper is purely for returning a list, and i understand that i would want to make sure that the list had records, I just dont know the syntax. Similarly, the main controller notDue getter - I know what I am trying to replicate (another list) and have tried this in my test class. The variable is being returned, however the actual result is not covered? Jan 22, 2019 at 1:42
  • Typically, when you query a list, you would access the list and write an assertion to ensure that the size of the list is equal to the number of records you made available in the test data (i.e., you would assert against list.size(). You may also write a negative assertion by asserting that a non-relevant record is not included.
    – David Reed
    Jan 22, 2019 at 1:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .