Skip to main content

I'm working on a test class and can't figure out why when I run the test I am not getting inside the for loop.  Could someone kindly help me troubleshoot?  

 

Apex Code:

********

public pagereference savefeedback()

    {                   

        updateflgusertb(Selected);

        return Auth.SessionManagement.FinishLoginflow('/');

    }

 

public pagereference saveandnext()

    {                   

        updateflgusertb(Selected);      

        return Auth.SessionManagement.FinishLoginflow('/apex/Application_RatingComments?id='+recid);

    }

 

public void updateflgusertb(string Selected)

    {

        List<Rating_Request__c> rateupdate = new List<Rating_Request__c>(); 

         List<Rating_Request__c> usr = [SELECT ID,Question__c,Order_Sequence__c,User_Rating__c,name 

                                       FROM Rating_Request__c where User__c=:UserInfo.getUserId() 

                                       and User_Rating__c = 0  order by name,Order_Sequence__c  LIMIT 1];

 

        for(Rating_Request__c objrating : usr)

        {

          if (Selected == null)

          {

             objrating.User_Rating__c = 1;

          }

          else

          {

             objrating.User_Rating__c = Decimal.valueOf(Selected); 

          }

            recid = objrating.Id;

        rateupdate.add(objrating);

        }

        update(rateupdate);

    }

 

Apex Test Class :

***************

 

@isTest

    static void savefeedback() 

    {

       Application_Rating smileyapex = new Application_Rating();

       PageReference feedback = smileyapex.savefeedback();  

       Test.setCurrentPage(feedback);

       System.assertEquals(null, feedback);

    }

 

@isTest

    static void saveandnext() 

    {

        Application_Rating smileyapex = new Application_Rating();

        pagereference saveref = smileyapex.saveandnext(); 

        Test.setCurrentPage(saveref);

        

        saveref.getParameters().put('Application_Name__c', 'FoxiPedia');

        pagereference saveobjpageRef = smileyapex.saveandnext();

        System.assertEquals(null, saveref);

    }

 

How to write test class to cover inside for loop if condition in apex test class.

18 answers
  1. Jan 30, 2023, 5:46 AM

    Your test class does not have any code to insert a "Rating_Request__c" record that can be retrieved by the "updateflgusertb" method. Without any data, the list "usr" in the "updateflgusertb" method is empty and the for loop will not execute.

    To cover the "if" condition inside the for loop, you need to insert a "Rating_Request__c" record and then call the "updateflgusertb" method. Here's an example of how you could modify your test class:

     

    @isTest

    static void savefeedbackTest() 

    {

        // Insert a Rating_Request__c record

        Rating_Request__c ratingRequest = new Rating_Request__c(Question__c = 'Question 1', Order_Sequence__c = 1, User__c = UserInfo.getUserId());

        insert ratingRequest;

        

        Application_Rating smileyapex = new Application_Rating();

        PageReference feedback = smileyapex.savefeedback();  

        Test.setCurrentPage(feedback);

        System.assertEquals(null, feedback);

     

        // Query the Rating_Request__c record to verify the value of User_Rating__c

        Rating_Request__c updatedRatingRequest = [SELECT ID, User_Rating__c FROM Rating_Request__c WHERE ID = :ratingRequest.ID];

        System.assertEquals(1, updatedRatingRequest.User_Rating__c);

    }

     

    @isTest

    static void saveandnextTest() 

    {

        // Insert a Rating_Request__c record

        Rating_Request__c ratingRequest = new Rating_Request__c(Question__c = 'Question 1', Order_Sequence__c = 1, User__c = UserInfo.getUserId());

        insert ratingRequest;

     

        Application_Rating smileyapex = new Application_Rating();

        PageReference saveref = smileyapex.saveandnext(); 

        Test.setCurrentPage(saveref);

        System.assertEquals(null, saveref);

     

        // Query the Rating_Request__c record to verify the value of User_Rating__c

        Rating_Request__c updatedRatingRequest = [SELECT ID, User_Rating__c FROM Rating_Request__c WHERE ID = :ratingRequest.ID];

        System.assertEquals(1, updatedRatingRequest.User_Rating__c);

    }

0/9000