+ Start a Discussion
kiran k 12kiran k 12 

How to test for loop in test class?

I have wriiten a test class for apex class.i got 89% but for loop is not covered.please see the screenshot for clarification.how to cover' for loop' and improve code coverage?
apex class:
public with sharing class Rfleet_Commericialcondition {
    //Variable Declaration Parts
    public List < RFLEET_Protocol_Grid__c > contt {get;set;}
    public List < EditableContact > myAssociatedContact {get;set;}
    public Integer editableContactNumber {get;set;}
    public Boolean refreshPage {get;set;}
    public String protocolname {get;set;}
    String id;
    //Constructor for invoking the Records from Protocol Grid Object
    public Rfleet_Commericialcondition(ApexPages.StandardController stdCtrl) {
            id = ApexPages.currentPage().getParameters().get('id');
            myAssociatedContact = new List < EditableContact > ();
            Integer counter = 0;
            RFLEET_Protocol__c conn = [select name from RFLEET_Protocol__c where id = : id];
            protocolname = conn.name;
            contt = [select id, Name, Rfleet_Type_of_Grid__c, Rfleet_Type_of_Sales__c, Rfleet_Protocol__c from RFLEET_Protocol_Grid__c where Rfleet_Protocol__r.name = : protocolname];

            for (RFLEET_Protocol_Grid__c myContact: contt) {
                myAssociatedContact.add(new EditableContact(myContact, false, counter));
                counter++;
            }
        }
        // This method is used for deleting the Row
    public void deleteRowEditAction() {
        try {
            myAssociatedContact.get(editableContactNumber).editable = false;
            delete(myAssociatedContact.get(editableContactNumber).myContact);
        } catch (Exception e) {}
        refreshPage = true;
    }
    public class EditableContact {
        public RFLEET_Protocol_Grid__c myContact {get;set;}
        public Boolean editable {get;set;}
        public Integer counterNumber {get;set;}
        public EditableContact(RFLEET_Protocol_Grid__c myContact, Boolean editable, Integer counterNumber) {
            this.myContact = myContact;
            this.editable = editable;
            this.counterNumber = counterNumber;
        }
    }
}
test class:
@isTest
public class Rfleet_Commercialcondition_Test {
    @isTest Static void Testcommercial(){
       
         RFLEET_Protocol_Grid__c myContact = new RFLEET_Protocol_Grid__c (Name='testing');
           insert myContact;
             myContact.Rfleet_Input_Mode__c = 'manually';
        update myContact;
            
            String Name = 'ListConditionCheck';
            Boolean editable; 
            Integer counterNumber;
            Boolean refreshPage;
            Integer editableContactNumber;        
            Rfleet_Commericialcondition.EditableContact wra= new Rfleet_Commericialcondition.EditableContact(myContact, editable, counterNumber);
            RFLEET_Protocol__c test = new RFLEET_Protocol__c(Name='prabu');
            insert test;
            test.Name = 'prabu';
            update test;
            System.debug('Before Query');
          
           
            RFLEET_Protocol__c myCondtest = new RFLEET_Protocol__c();
            myCondtest = [select id,Name from RFLEET_Protocol__c LIMIT 1];
            PageReference vfpage = Page.Rfleet_Commericialcondition;
            System.test.SetCurrentpage(vfpage);
            Apexpages.currentPage().getparameters().put('id',myCondtest.id);
            System.assertEquals(myCondtest.id,ApexPages.currentPage().getParameters().get('id'));
            Apexpages.StandardController sc = new Apexpages.StandardController(myCondtest);
            Rfleet_Commericialcondition commtest = new Rfleet_Commericialcondition(sc);
            commtest.deleteRowEditAction();
            System.assertEquals('prabu',test.Name);
        
               
    }
    
}
red color mark in test class

 
Vivek_PatelVivek_Patel
Hi Kiran,


That loop is not covered, because the query above the loop is not returing records, make sure the records satisfy the criteria in where clause and then try again. If the query returns even a single record, that loop will be covered.
Sagar PareekSagar Pareek

In your test class remove query at line 24.This query do not have access to database because you are not using SeeAllData=true with your test class (and which i am not recommending to use) instead try to use ids of the records you have inserted/updated in the test class. You have inserted and updated RFLEET_Protocol_Grid__c records. Use these records ids at line 27 of your test class. It should be like this
Apexpages.currentPage().getparameters().put('id',test.id);
kiran k 12kiran k 12
hi] Sagar Pareek,
I used this one.But got the same percentage.Inside for loop it is not covered.
ManojjenaManojjena
HI Kiran,
To cover for loop your query should return record in test method .So Try to modify your test class like belwo means add belwo line in your test class accordingly .
 
Apexpages.StandardController sc = new Apexpages.StandardController(myCondtest);
Rfleet_Commericialcondition commtest = new Rfleet_Commericialcondition(sc);
commtest.protocolname ='prabu';
As your condition is based on your variable.
Let me know if it helps !!
Thanks
Manoj



 
kiran k 12kiran k 12
As your condition base, i used "    commtest.protocolname ='prabu';  ".But it is same as before
Sameer PrasonnSameer Prasonn
Hi Kiran,

I believe the test data doesn't match with the criteria, As the test code shows We don't populate test data for 'myContact.RFLEET_Protocol_Grid__c' field in 'RFLEET_Protocol_Grid__c' object. If we have so it will cover the loop.

Please review following lines of code
RFLEET_Protocol__c conn = [select name from RFLEET_Protocol__c where id = : id];
protocolname = conn.name;
contt = [select id, Name, Rfleet_Type_of_Grid__c, Rfleet_Type_of_Sales__c, Rfleet_Protocol__c from RFLEET_Protocol_Grid__c where Rfleet_Protocol__r.name = : protocolname];
Now also review following lines as well in test class.
 
RFLEET_Protocol_Grid__c myContact = new RFLEET_Protocol_Grid__c (Name='testing');
insert myContact;
myContact.Rfleet_Input_Mode__c = 'manually';
update myContact;
String Name = 'ListConditionCheck';
Boolean editable;
Integer counterNumber;
Boolean refreshPage;
Integer editableContactNumber;        
Rfleet_Commericialcondition.EditableContact wra= new Rfleet_Commericialcondition.EditableContact(myContact, editable, counterNumber);
RFLEET_Protocol__c test = new RFLEET_Protocol__c(Name='prabu');
insert test;
test.Name = 'prabu'; 
update test;
System.debug('Before Query');

what we need is just following code need to add right after last statement
 
myContact.RFLEET_Protocol_Grid__c=test
​update myContact

then I believe it run the loop once since we will recieve one record related to the criteria.
Please mark best answer if it resolve your query.