You need to sign in to do that
Don't have an account?
SazEast
help writing test class for a wrapper class which retrieves grandchild records
I'm getting a 'variable does not exist' error but it is in the wrapper class so i'm not sure what I'm doing wrong, please could you help with the test class code so I can deploy? Thanks
Wrapper Class:
Test Class:
Wrapper Class:
public class WrapperSarah{ public Consolidated_Invoicing__c consinvs; public WrapperSarah(ApexPages.StandardController stdController) { this.consinvs = (Consolidated_Invoicing__c)stdController.getRecord(); WrapperSarah wrp = new WrapperSarah(); } Id recordId; public List<Consolidated_Invoicing__c> consolidatedInvoiceList { get; set; } public List<Invoice__c> invoiceList { get; set; } public Map<Id, List<Invoice__c>> coninvIdinvListMap { get ; set; } Set<Id> invoiceIds = new Set<Id>(); Id Consolidated_InvoiceId = ApexPages.currentPage().getParameters().get('consId'); Map<Id, Consolidated_Invoicing__c> memberMap = new Map<Id, Consolidated_Invoicing__c>(); public List<ConsolidatedInvoiceWrapperSarah> consolidatedInvoiceWrapperSarahList { get; set; } public WrapperSarah() { consolidatedInvoiceList = [SELECT Total__c, Name FROM Consolidated_Invoicing__c Where Id =: ApexPages.currentPage().getParameters().get('id')]; coninvIdinvListMap = new Map<Id, List<Invoice__c>>(); consolidatedInvoiceWrapperSarahList = new List<ConsolidatedInvoiceWrapperSarah>(); if(consolidatedInvoiceList.size() > 0) { for (Consolidated_Invoicing__c cons: consolidatedInvoiceList) { invoiceIds.add(cons.Id); memberMap.put(cons.Id, cons); } invoiceList = [SELECT Name, Consolidated_Invoice__c, Patient_Name__c, PatientNameString__c, Hospital_Treated__c, Policy_Number__c, Consultant__c, DOB__c, (SELECT Name, Charge_Description__c, Charge_Code__c, Net_Price__c, Gross_Price__c, Line__c, Quantity__c, VAT__c, Date__c FROM Treatments__r) FROM Invoice__c WHERE Consolidated_Invoice__c IN : invoiceIds]; system.debug('Invoice List is ' + invoiceList); } if(invoiceList.size() > 0) { for(Invoice__c intrst : invoiceList) { if(!coninvIdinvListMap.containsKey(intrst.Consolidated_Invoice__c)){ coninvIdinvListMap.put(intrst.Consolidated_Invoice__c, new List<Invoice__c>()); } coninvIdinvListMap.get(intrst.Consolidated_Invoice__c).add(intrst); } for(Id invoiceId : coninvIdinvListMap.keySet()) { consolidatedInvoiceWrapperSarahList.add(new ConsolidatedInvoiceWrapperSarah(memberMap.get(invoiceId), coninvIdinvListMap.get(invoiceId))); } } } public class ConsolidatedInvoiceWrapperSarah { public Consolidated_Invoicing__c consinv { get; set; } public List<Invoice__c> invclist { get; set; } public ConsolidatedInvoiceWrapperSarah(Consolidated_Invoicing__c consinv, List<Invoice__c> invclist) { this.consinv= consinv; this.invclist = invclist; } } }
Test Class:
@IsTest private class TestWrapperSarah { @IsTest private static void WrapperSarah(){ Consolidated_Invoicing__c ci = new Consolidated_Invoicing__c(status__c='sent'); insert ci; string invclist=ci.ID; Invoice__c ni = new Invoice__c(); ni.Consolidated_Invoice__c = ci.Id; ni.Name = 'Fake'; insert ni; Treatment__c nt = new Treatment__c(); nt.Invoice__c = ni.Id; nt.Charge_Code__c = 'Fake Code'; nt.Date__c = Date.Today(); nt.Net_Price__c = 100.10; insert nt; Test.StartTest(); WrapperSarah co = new WrapperSarah(); system.assertEquals(1,WrapperSarah.invclist.size()); Test.stopTest(); } }
@sarah can you post error here after try below code,
I had added your suggestion and amended slightly which saves with no 'problems' however the test shows as "System.NullPointerException: Attempt to de-reference a null object"
I think this may be because I haven't declared the Name under Consolidated Invoicing? It won't allow me to write to this field though because it is an auto-number. Do you know how I can get around this?