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
SazEastSazEast 

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:
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();      
           

     }    
}

 
dandamudidandamudi

@sarah  can you post error here after try below code, 
@IsTest private class TestWrapperSarah {
    @IsTest
       private static void WrapperSarah(){
	   Consolidated_Invoicing__c ci = new Consolidated_Invoicing__c(status__c='sent');
			insert ci;
	   PageReference pf = Page.WrapperSarah1;
	   Test.setCurrentPage(pf);
	   pf.getParameters().put('Id', String.valueOf(ci.Id))
           
	   WrapperSarah controller = new WrapperSarah(new ApexPages.StandardController(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();      
           

     }    
}

 
SazEastSazEast
Thanks @dandamudi

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? 
 
@IsTest private class TestWrapperSarah {
    @IsTest
       private static void WrapperSarah(){
        
           
           Patient__c NP = new Patient__c(EMPI__c='12312311',Name='Bob');
           Third_Parties__c ct = new Third_Parties__c(name='TestTP');
             insert NP;  
           insert ct;
           
           Financial_Class_Map__c fc = new Financial_Class_Map__c();
      	    fc.Name = 'UG1';
        	fc.Financial_Class_Name__c = 'Test Financial';
           insert fc;
           
           Consolidated_Invoicing__c ci = new Consolidated_Invoicing__c(status__c='sent',Third_Party__c=ct.Id, Financial_Class__c=fc.id);
           
			insert ci;
           
            
      	   WrapperSarah controller = new WrapperSarah(new ApexPages.StandardController(ci));
                     
           
           string invclist=ci.ID;
           
           Invoice__c ni = new Invoice__c(Patient_Name__c=NP.Id);
           ni.Consolidated_Invoice__c = ci.Id;
           ni.Financial_Class__c = fc.Id;
		   ni.Patient_Name__c = NP.Id;
		   ni.Third_Party_Account__c = ct.Id;           
           ni.Name = 'Fake';
           insert ni;
           
           Treatment__c nt = new Treatment__c();
           nt.Invoice__c = ni.Id;
           nt.Charge_Code__c = 'AABB';
           nt.Date__c = Date.Today();
           nt.Net_Price__c = 100.10;
           insert nt;
           
                PageReference pf = Page.apexdatatableinsertforCONSINV;
	   Test.setCurrentPage(pf);
	   pf.getParameters().put('Id', String.valueOf(nt.Id));
           
              Test.StartTest();
           
	WrapperSarah co = new WrapperSarah();
      
           system.debug(pf);
          system.assertEquals(1,WrapperSarah.ci.size());
           
           Test.stopTest();      

 

     }