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
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student 

How to call the wrapper parts of a wrapper class in test - Only passing 73% - Have I even formed it right?

Hi there,

I have a wrapper class and I am trying to test it. I need 100% as my other code coverage isa  bit lacking, but only have 73%. I am not sure if I have even set this out properly and I dont know how to test for the wrapper parts of my controller. E.G. FinLoanWrapper, any help would be muich appreciated.

Test
@isTest 
private class FinanceNew_Test{
    static testMethod void FinanceCommissionNew() {
    
     PageReference pageRef = Page.DestinyFinanceNew4;
        Test.setCurrentPage(pageRef);
    
    Office__c Office = new Office__c(Name = 'Test Pro Account');
insert Office;
Account Acc = new Account(Name = 'Test Account', office__c = Office.id);
insert Acc;
Finance__c Fin = new Finance__c(Finance_Office__c = office.id, Account__c = Acc.id);
insert Fin;
List<Finance_Loan_Security__c> FLSEC = new List<Finance_Loan_Security__c>{};
for(Integer i = 0; i < 10; i++){
        Finance_Loan_Security__c FLSE = new Finance_Loan_Security__c(Office__c = Office.id, Account__c = acc.id, No_Street__c = 'Test' + i, Finance__c = Fin.id);
        FLSEC.add(FLSE);
    }
List<Finance_Loan_Split__c> FLSPL = new List<Finance_Loan_Split__c>{};
for(Integer i = 0; i < 10; i++){
        Finance_Loan_Split__c FLS = new Finance_Loan_Split__c(Office__c = Office.id, Account__c = acc.id, Account_Number__c = 'Test' + i, Finance__c = Fin.id);
        FLSPL.add(FLS);
    }
    insert FLSPL;
    Insert FLSEC;
    
    ApexPages.currentPage().getParameters().put('FinId', Fin.id);
     ApexPages.currentPage().getParameters().put('OffId', Office.id);
      ApexPages.currentPage().getParameters().put('AccId', Acc.id);


       //this call tests the constructor:
       FinanceNew controller = new FinanceNew();
       
      
       //test building was complete
        System.assert(controller.wrappers.size()==10);
         System.assert(controller.wrappers2.size()==10);
        
        
        //call the pageReference in the class.
        controller.FinLoanCalculate();
        controller.FinSecCalculate();
        System.assert(Fin.Aggregate_Borrowings__c ==0.00);
        
        controller.addRows();
        controller.delWrapper();
        controller.addRows2();
        controller.delWrapper2();
        
        controller.saveStandard();
        controller.Cancel();



    }
}



Class
public class FinanceNew{
    
    public Finance__c Fin { get; set; }
    public Finance_Loan_Security__c LoanSecurity { get; set; }
    public Finance_Loan_Split__c LoanSplit { get; set; }
    
    
    //Wrapper multi add try to implement
     public List<FinLoanSplitWrapper> wrappers {get; set;}
 public static Integer toDelIdent {get; set;}
 public static Integer addCount {get; set;}
 private Integer nextIdent=0;
 
 //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Wrapper 2 identical - 1
       public List<FinLoanSecurityWrapper> wrappers2 {get; set;}
 public static Integer toDelIdent2 {get; set;}
 public static Integer addCount2 {get; set;}
 private Integer nextIdent2=0;
 
 //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Wrapper 2 identical - 1
  Id FinId = ApexPages.currentPage().getParameters().get('FinId');
   public string delId {get;set;}
 ////////////////test
 

 
 public void FinLoanCalculate()
{
nextIdent=-1;
Integer NoSplits = 0;
Decimal AggBorrowing = 0.00;
  for (FinLoanSplitWrapper wrap : wrappers)
  {
  if(wrap.FinLoanS.Loan_Amount__c == null){
  wrap.FinLoanS.Loan_Amount__c = 0.00;
  }
  AggBorrowing = AggBorrowing + wrap.FinLoanS.Loan_Amount__c;
  NoSplits = NoSplits + 1;
  nextIdent = nextIdent + 1;
  wrap.ident = nextIdent;
  }
  Fin.Number_of_Splits__c = NoSplits;
  Fin.Aggregate_Borrowings__c = AggBorrowing;


}

 public void FinSecCalculate()
{
nextIdent2=-1;
Integer NoSecurities = 0;
Decimal ValTotal = 0.00;
  for (FinLoanSecurityWrapper wrap : wrappers2)
  {
  if(wrap.FinSecS.Actual_Value__c== null){
  wrap.FinSecS.Actual_Value__c= 0.00;
  }
  ValTotal = ValTotal  + wrap.FinSecS.Actual_Value__c;
  NoSecurities = NoSecurities + 1;
  nextIdent2 = nextIdent2 + 1;
  wrap.ident2 = nextIdent2;
  }
  Fin.Number_of_Securities__c = NoSecurities;
   Fin.total_security_Value__c= ValTotal;
}



 ////////////////test









////////////////////Delete Rows
//Delete Rows 1 
public void delWrapper()
 {
  Integer toDelPos=-1;
  boolean IsInsertedRow = False;
  for (Integer idx=0; idx<wrappers.size(); idx++)
  {
   if (wrappers[idx].ident==toDelIdent)
   {
    toDelPos=idx;
   }
    if (wrappers[idx].IsInserted==true){
    IsInsertedRow = true;
    }
  }
   
  if (-1!=toDelPos)
  {
   wrappers.remove(toDelPos);
  }
  if(delId != null && IsInsertedRow == True){
  
  Finance_Loan_Split__c[] FLS = [select id from Finance_Loan_Split__c where id =: delId ];
    if(FLS.size() > 0){
  
  delete FLS[0];
  
  }
  }
  delId = null;
  IsInsertedRow = false;
  
 }



//Delete Rows 2
  public void delWrapper2()
 {
  boolean IsInsertedRow = False;
  Integer toDelPos=-1;
  for (Integer idx=0; idx<wrappers2.size(); idx++)
  {
   if (wrappers2[idx].ident2==toDelIdent2)
   {
    toDelPos=idx;
   }
    if (wrappers2[idx].IsInserted==true){
    IsInsertedRow = true;
    }
  }
   
  if (-1!=toDelPos)
  {
   wrappers2.remove(toDelPos);
  }
  if(delId != null && IsInsertedRow == True){
   Finance_Loan_Security__c[] FLSE = [select id from Finance_Loan_Security__c where id =: delId ];
         if(FLSE.size() > 0){
  
  delete FLSE[0];
  
  }
  }
 }







 //////////////////////////Add Rows
 //Add Rows 1
 public void addRows()
 {
  for (Integer idx=0; idx<addCount; idx++)
  {
   wrappers.add(new FinLoanSplitWrapper(integer.valueof(Fin.Number_of_Splits__c), null));
  }
 }
//Add Rows 2
 public void addRows2()
 {
  for (Integer idx=0; idx<addCount2; idx++)
  {
   wrappers2.add(new FinLoanSecurityWrapper(integer.valueof(Fin.Number_of_Securities__c),null));
  }
 }
  //////////////////////////Add Rows
 
 

 
 
 ////////////////Wrappers
 public class FinLoanSplitWrapper
 {
  public Finance_Loan_Split__c FinLoanS {get; private set;}
  public Integer ident {get; private set;}
  public boolean IsInserted  {get; set;}
   
  public FinLoanSplitWrapper(Integer inIdent , Finance_Loan_Split__c LoanSplit)
  {
  ident=inident;
 if(LoanSplit != null){
   FinLoans = LoanSplit; 
   IsInserted = true;    
   }else{
   FinLoanS=new Finance_Loan_Split__c(Loan_Split_Number__c=ident);
   IsInserted = false;
   }
  }
 }
  
 public class FinLoanSecurityWrapper
 {
  public Finance_Loan_Security__c FinSecS {get; private set;}
  public Integer ident2 {get; private set;}
  public boolean IsInserted  {get; set;}
   
  public FinLoanSecurityWrapper(Integer inIdent, Finance_Loan_Security__c LoanSecurity )
  {
   ident2=inIdent;
 if(LoanSecurity != null){
     FinSecS = LoanSecurity;
     IsInserted = true;  
     }else{
   FinSecS=new Finance_Loan_Security__c(Loan_Security_Number__c=ident2);
   IsInserted = false;
   }
   
   
  }
 }
  

 public FinanceNew() {
     
     Id FinId = ApexPages.currentPage().getParameters().get('FinId');
     if(FinId != null){
     Fin = [SELECT Id, Application_End_Reason__c, Financier__c, Financier_Reference__c, Application_LVR__c ,
     Total_Security_Value__c, Loan_Information__c , Account__c , Finance_Office__c, Settlement_Due__c, Finance_Clause__c, Number_of_Securities__c,Number_of_Splits__c ,
     Aggregate_Borrowings__c, Total_New_Loans__c, Application_Completed__c ,Application_Submitted__c,Conditional_Approval__c,Valuation_Completed__c,
     Unconditional_Approval__c ,Finance_Documents_Issued__c,Finance_Documents_Executed__c ,Finance_Documents_Submitted__c ,Finance_Certified__c , Finance_Settlement__c ,
     Application_End__c,Upfront_Commission_Paid__c       FROM Finance__c
                   WHERE Id = :ApexPages.currentPage().getParameters().get('FinId') ];
                
 if(wrappers == null) { 
     
      wrappers=new List<FinLoanSplitWrapper>();
      for(Finance_Loan_Split__c LoanSplit:[SELECT Id, Account__c ,Office__c, Loan_Split_Number__c, Loan_Amount__c ,Loan_Type__c,Payment_Type__c,
      Loan_Purpose__c,Comment__c, finance__c, Secondary_Purpose__c, Account_Number__c  FROM Finance_Loan_Split__c
                   WHERE finance__c  = :FinId order by Loan_Split_Number__c asc]){
                   
                   nextIdent = Integer.valueof(LoanSplit.Loan_Split_Number__c);
                   
                 wrappers.add(new FinLoanSplitWrapper(nextIdent,LoanSplit));
                   }
                   }
                  
 if(wrappers2 == null) { 
     
      wrappers2=new List<FinLoanSecurityWrapper>();
      for(Finance_Loan_Security__c LoanSecurity :[SELECT Id, Account__c ,Office__c,Loan_Security_Number__c,New_Existing__c,Estimated_Value__c,No_Street__c,
      Suburb__c,State__c ,Postcode__c ,Actual_Value__c,Valuation_Date__c , Rental_Yield__c,Finance__c,Loan_Priority__c,Title_Owner__c   FROM Finance_Loan_Security__c
                   WHERE finance__c  = :FinId order by Loan_Security_Number__c asc]){
                   
                   nextIdent2 = Integer.valueof(LoanSecurity.Loan_Security_Number__c);
                   
                 wrappers2.add(new FinLoanSecurityWrapper(nextIdent2,LoanSecurity));
                   }
                   }
                
    
     
     }else{
     Fin = new Finance__c (Number_of_Splits__c = 1,Number_of_Securities__c = 1,Aggregate_Borrowings__c = 0.00, Total_New_Loans__c = 0.00, Total_Security_Value__c = 0.00);
     Fin.Finance_Office__c = ApexPages.currentPage().getParameters().get('OffId');
     Fin.Account__c = ApexPages.currentPage().getParameters().get('AccId');
      
      
    //LoanSecurity = new Finance_Loan_Security__c ();
      wrappers=new List<FinLoanSplitWrapper>();
  for (Integer idx=0; idx<1; idx++)
  {
   wrappers.add(new FinLoanSplitWrapper(nextIdent++, null));
  }
      wrappers2=new List<FinLoanSecurityWrapper>();
  for (Integer idx=0; idx<1; idx++)
  {
   wrappers2.add(new FinLoanSecurityWrapper(nextIdent2++,null));
  }

}  
      
    }
    
//////////////////////////Save and Cancel\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\    
    public PageReference saveStandard() {
    
    List<Finance_Loan_Split__c> FLS =new List<Finance_Loan_Split__c>();
 Integer SetFLS = -1;
  for (FinLoanSplitWrapper wrap : wrappers)
  {
  SetFLS = SetFLS + 1;
   wrap.FinLoanS.Loan_Split_Number__c = SetFLS;
   FLS.add(wrap.FinLoanS);
  }
  Fin.Number_of_Splits__c = SetFLS+1;

 List<Finance_Loan_Security__c> LoanSecurity =new List<Finance_Loan_Security__c>();
 Integer SetFLSE = -1;
  for (FinLoanSecurityWrapper wrap : wrappers2)
  {
   SetFLSE = SetFLSE + 1;
   wrap.FinSecS.Loan_Security_Number__c = SetFLSE;
   LoanSecurity.add(wrap.FinSecS);
  }
  Fin.Number_of_Securities__c= SetFLSE+1;
    
     try {  
      upsert Fin;
        
        } catch (Exception e) {     
      
             ApexPages.addMessages(e);         
        }
        

  
  for (Finance_Loan_Split__c FinLoanSplit: FLS)
  {
  If(FinLoanSplit.Finance__c == null){
    FinLoanSplit.Finance__c = Fin.id;
    }
  If(FinLoanSplit.Account__c == null){
   FinLoanSplit.Account__c = Fin.Account__c;
   }
   If(FinLoanSplit.Office__c == null){
   FinLoanSplit.Office__c = Fin.Finance_Office__c;
   }
  }
  
    for (Finance_Loan_Security__c LoanSec: LoanSecurity)
  {
   If(LoanSec.Finance__c == null){
    LoanSec.Finance__c = Fin.id;
    }
    If(LoanSec.Account__c == null){
   LoanSec.Account__c = Fin.Account__c;
   }
   If(LoanSec.Office__c == null){
   LoanSec.Office__c = Fin.Finance_Office__c;
   }
  }            
        
        try { 
        
        upsert FLS;
        upsert LoanSecurity;
       
       
        PageReference pageRef= new PageReference('/apex/DestinyAccount?id='+fin.Account__c+'&Sfdc.override=1');
        return pageRef;
        } catch (Exception e) {     
      
             ApexPages.addMessages(e);         
        }
        
        
        
        return null;
    
    }
    
      
    public PageReference Cancel() {
   
        PageReference pageRef= new PageReference('/apex/DestinyAccount?id='+Fin.account__c+'&Sfdc.override=1');
        return pageRef;
     
    }
//////////////////////////Save and Cancel\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\    
//////////////////////Old Code to keep for Reference - Delete Later\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\   


  /* Remove for now, as it has been made redunant. keep for reference incase soemthing goes wrong
 public PageReference save()
 {
  List<Finance_Loan_Split__c> FLS =new List<Finance_Loan_Split__c>();
  for (FinLoanSplitWrapper wrap : wrappers)
  {
   FLS.add(wrap.FinLoanS);
  }
   
  insert FLS;
   
  return new PageReference('/' + Schema.getGlobalDescribe().get('Finance_Loan_Split__c').getDescribe().getKeyPrefix() + '/o');
 }
 */

//////////////////////Old Code to keep for Reference - Delete Later\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\    
 
    
}

 
SForceBeWithYouSForceBeWithYou
Since the wrapper class is pretty standalone, you can add test methods to cover them.  Something like this should do:
@isTest 
private class FinanceNew_Test{
    static testMethod void FinanceCommissionNew() {
        [... existing test method ...]
    }
    
    static testMethod void testFinLoanSplitWrapper() {
        Integer yourInIndent = 1;
        Finance_Loan_Split__c finLoanSplit = new Finance_Loan_Split__c();
        FinLoanSplitWrapper splitWrapper = new FinLoanSplitWrapper(yourInIndent, finLoanSplit);
    }
    
    static testMethod void testFinLoanSecurityWrapper() {
        Integer yourInIndent = 1;
        Finance_Loan_Security__c finLoanSec = new Finance_Loan_Security__c();
        FinLoanSecurityWrapper secWrapper = new FinLoanSecurityWrapper(yourInIndent, finLoanSec);
    }
}

Note: It is MUCH better to get business use cases covered and assert that the right outcomes happen.  Covering lines of code and not checking anything is kind of like a teacher looking over your test answers and not checking them against the answer key.
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

Awesome advice right there, thank you for your time.

The thing is, I would test for these business situations, except I am not sure how to phrase the testing. Example, were I to test 'FinLoanCalculate' using system assert. I could try :

 

  controller.FinLoanCalculate();
        System.assertequals(Fin.Aggregate_Borrowings__c , null);

I had to set it as null, so it would pass. I gave  each one of the 10 loan splits 1 dollar and after called the void, it did not change the field. Even though, when I test it on the page (and I have tested very thoroughly) it works fine. I figure it is because I am not calling it properly.

I also attempted to save the new tests that you suggested. It gave me this error: Error: Compile Error: Invalid type: FinLoanSplitWrapper at line 62 column 48...I am not sure why.

Thank you for your time so far.