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