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
ard_17ard_17 

Not able to get inside for loop when testing

I have three for loops in my controller and when I run the test, I get code coverage everywhere except inside of the three for loops. Can someone point me in the right direction?
 
public with sharing class applyPayments_Controller {
    public List<InvoiceWrapper> searched_invoices {get;set;}
    public List<gii__OrderInvoice__c> selectedInvoices {get;set;}
    public string accountName {get;set;}
    public boolean viewTable;
    public date paymentDate {get;set;}
    public string paymentMethod {get;set;}
    public string reference {get;set;}
    public decimal amount {get;set;}
    public string comments {get;set;}
    
    public Boolean getViewTable()
    {
        return this.viewTable;
    }
    
    public applyPayments_Controller()
    {
        viewTable=false;
        searched_invoices = new List<InvoiceWrapper>();
    }
    
    public void searchInvoices()
    {   
        string likeAccountName = '%' + accountName + '%';
        searched_invoices.clear();
        for(gii__OrderInvoice__c i: [select Id, Name, Total_Invoice__c, gii__BalanceAmount__c, Past_Due__c, Account_Name__c, gii__Status__c from gii__OrderInvoice__c 
                                     where (Account_Name__c LIKE :likeAccountName AND gii__Status__c != 'Cancelled' AND Past_Due__c != 'PAID')
                                     order by Account_name__c, Name])
        {
            searched_invoices.add(new InvoiceWrapper(i));
        }
        
        viewTable = true;
    }
    
    public PageReference Next()
    {
        selectedInvoices = new List<gii__OrderInvoice__c>();
        selectedInvoices.clear();
        for (InvoiceWrapper wrapInvoiceObj : searched_invoices)
        {
            if(wrapInvoiceObj.selected == true)
            {
                selectedInvoices.add(wrapInvoiceObj.inv);
            }
        }
        
        paymentDate = System.Today();
        PageReference applyPaymentsVF_2 = new PageReference('/apex/applyPaymentsVF_2');
        return applyPaymentsVF_2;
    }
    
    public void applyPayment()
    {
        for(gii__OrderInvoice__c inv : selectedInvoices)
        {
            gii__ARInvoicePayment__c payment = new gii__ARInvoicePayment__c();
            payment.gii__Invoice__c = inv.id;
            payment.gii__Reference__c = reference;
            payment.gii__Comments__c = comments;
            payment.gii__PaymentMethod__c = paymentMethod;
            payment.gii__PaymentDate__c = paymentDate;
            payment.gii__PaidAmount__c = inv.gii__BalanceAmount__c;
            insert payment;
        }    
    }
    
    //This is wrapper/container class
    public class InvoiceWrapper
    {
        public String accName {get;set;}
        public String invName {get;set;}
        public decimal total {get;set;}
        public decimal balance {get;set;}
        public String status {get;set;}
        public gii__OrderInvoice__c inv {get;set;}
        public Boolean selected {get;set;}
        public InvoiceWrapper(gii__OrderInvoice__c i)
        {
            inv = i;
            selected = false;
            accName = i.Account_Name__c;
            invName = i.Name;
            total = i.Total_Invoice__c;
            balance = i.gii__BalanceAmount__c;
            status = i.Past_Due__c;
        }
    } 
}
 
@isTest
public class applyPayments_ControllerTest {
    public static testMethod void testMyController()
    {  
        gii__SalesOrder__c so = new gii__SalesOrder__c();
        Id RecordTypeIdSO = Schema.SObjectType.gii__SalesOrder__c.getRecordTypeInfosByName().get('Service Call Order').getRecordTypeId();
        so.RecordTypeId = RecordTypeIdSO;
        so.gii__OrderType__c = 'Standard';
        insert so;
        
        gii__OrderInvoice__c inv = new gii__OrderInvoice__c();
        inv.gii__SalesOrder__c = so.Id;
        insert inv;
        
        applyPayments_Controller controller = new applyPayments_Controller();
        applyPayments_Controller.InvoiceWrapper wrp = new applyPayments_Controller.InvoiceWrapper(inv);
        controller.searchInvoices();
        controller.getViewTable();
        controller.Next();
        controller.applyPayment();
    }
}

 
Best Answer chosen by ard_17
Abdul KhatriAbdul Khatri
Please try this
 
@isTest
public class applyPayments_ControllerTest {
    public static testMethod void testMyController()
    {  
        gii__SalesOrder__c so = new gii__SalesOrder__c();
        Id RecordTypeIdSO = Schema.SObjectType.gii__SalesOrder__c.getRecordTypeInfosByName().get('Service Call Order').getRecordTypeId();
        so.RecordTypeId = RecordTypeIdSO;
        so.gii__OrderType__c = 'Standard';
        insert so;
        
        gii__OrderInvoice__c inv = new gii__OrderInvoice__c();
        inv.gii__SalesOrder__c = so.Id;
        inv.Account_Name__c = 'Test';
        insert inv;
        
        applyPayments_Controller controller = new applyPayments_Controller();
        applyPayments_Controller.InvoiceWrapper wrp = new applyPayments_Controller.InvoiceWrapper(inv);

        controller.Account_Name__c = 'Test'
        controller.searchInvoices();
        
        controller.searched_invoices.get(0).selected = true;
        
        controller.getViewTable();
        controller.Next();
        controller.applyPayment();
    }
}

 

All Answers

Abdul KhatriAbdul Khatri
Please check below if this somehow help
 
@isTest
public class applyPayments_ControllerTest {
    public static testMethod void testMyController()
    {  
        gii__SalesOrder__c so = new gii__SalesOrder__c();
        Id RecordTypeIdSO = Schema.SObjectType.gii__SalesOrder__c.getRecordTypeInfosByName().get('Service Call Order').getRecordTypeId();
        so.RecordTypeId = RecordTypeIdSO;
        so.gii__OrderType__c = 'Standard';
        insert so;
        
        gii__OrderInvoice__c inv = new gii__OrderInvoice__c();
        inv.gii__SalesOrder__c = so.Id;
        inv.Account_Name__c = 'Test';
        insert inv;
        
        applyPayments_Controller controller = new applyPayments_Controller();
        applyPayments_Controller.InvoiceWrapper wrp = new applyPayments_Controller.InvoiceWrapper(inv);
        wrp.selected = true;
        controller.Account_Name__c = 'Test'
        controller.searchInvoices();
        controller.getViewTable();
        controller.Next();
        controller.applyPayment();
    }
}

 
ard_17ard_17
Abdul,

Thank you for your response! With the changes I was able to get inside the first for loop, but not the other two.
Abdul KhatriAbdul Khatri
Please try this
 
@isTest
public class applyPayments_ControllerTest {
    public static testMethod void testMyController()
    {  
        gii__SalesOrder__c so = new gii__SalesOrder__c();
        Id RecordTypeIdSO = Schema.SObjectType.gii__SalesOrder__c.getRecordTypeInfosByName().get('Service Call Order').getRecordTypeId();
        so.RecordTypeId = RecordTypeIdSO;
        so.gii__OrderType__c = 'Standard';
        insert so;
        
        gii__OrderInvoice__c inv = new gii__OrderInvoice__c();
        inv.gii__SalesOrder__c = so.Id;
        inv.Account_Name__c = 'Test';
        insert inv;
        
        applyPayments_Controller controller = new applyPayments_Controller();
        applyPayments_Controller.InvoiceWrapper wrp = new applyPayments_Controller.InvoiceWrapper(inv);

        controller.Account_Name__c = 'Test'
        controller.searchInvoices();
        
        controller.searched_invoices.get(0).selected = true;
        
        controller.getViewTable();
        controller.Next();
        controller.applyPayment();
    }
}

 
This was selected as the best answer
ard_17ard_17
That was it!! Thank you so much!