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
hramanihramani 

Test Class coverage Help NEEDED TWO LINES

I have the below code and its test class.
There are two lines that are not getting covered in the Test class. Please help me.

CLASS

global without sharing class RecommendationEmailUpdate {

    @RemoteAction
    global static List<TargetX_SRMb__Recommendation__c> getRecommendations(String applicationId){
        List<TargetX_SRMb__Recommendation__c> recommendations = [Select Id, EmailSent__c, TargetX_SRMb__Application__c  from TargetX_SRMb__Recommendation__c where TargetX_SRMb__Application__c = :applicationId];
        return recommendations;
    } 

    public void recommendationEmailFieldUpdate(List<TargetX_SRMb__Recommendation__c> recList){
        List<TargetX_SRMb__Recommendation__c> recLists = new List<TargetX_SRMb__Recommendation__c> ();
        for(TargetX_SRMb__Recommendation__c recVal : recList) {
            recVal.EmailSent__c = 'Yes';
            recLists.add(recVal);
        }
        update recLists;
    }
}


TEST CLASS

@isTest
private class RecommendationEmailUpdateTest {

    @testSetup 
    static void setup() {
        List<TargetX_SRMb__Recommendation__c> recLists = new List<TargetX_SRMb__Recommendation__c> ();
        Contact con = new Contact();
        con.LastName = 'TestContact';
        insert con;
        TargetX_SRMb__Application__c app = new TargetX_SRMb__Application__c();
        app.TargetX_SRMb__Contact__c = con.Id;
        insert app;
        for (Integer i = 0; i < 2; i++ ) {
            TargetX_SRMb__Recommendation__c rec = new TargetX_SRMb__Recommendation__c();
            rec.Name = 'RecommendationTest'+i;
            rec.TargetX_SRMb__Email__c = 'test'+i+'@test.com';
            rec.TargetX_SRMb__Contact__c = con.Id;
            rec.TargetX_SRMb__Application__c = app.Id;
        }
        insert recLists;
    }

    private static testMethod void getRecommendationsTest() {
        TargetX_SRMb__Application__c application = [Select Id from TargetX_SRMb__Application__c limit 1];
        String applicationId = String.ValueOf(application.Id);
    
        Test.startTest();
        RecommendationEmailUpdate.getRecommendations(applicationId);
        Test.stopTest();  
    }
    
    private static testMethod void recommendationEmailFieldUpdateTest() {
        List<TargetX_SRMb__Recommendation__c> recommendations = [Select Id, EmailSent__c from TargetX_SRMb__Recommendation__c];
        Test.startTest();
        RecommendationEmailUpdate emailFieldUpdate = new RecommendationEmailUpdate();
        emailFieldUpdate.recommendationEmailFieldUpdate(recommendations);
        Test.stopTest();
    } 
}


TWO LINES  NOT GETTING COVERED ARE :

recVal.EmailSent__c = 'Yes';
recLists.add(recVal);



 
Deepali KulshresthaDeepali Kulshrestha
Hi hramani,

  List<TargetX_SRMb__Recommendation__c> recLists = new List<TargetX_SRMb__Recommendation__c> ();

        for(TargetX_SRMb__Recommendation__c recVal : recList) 

Your list name is 'recLists' and in for loop you are using 'recList' , change the name of list and it will be fine.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.
Maharajan CMaharajan C
HI hramani.

In Test setup you missed out to add the  TargetX_SRMb__Recommendation__c records in recLists.

    @testSetup 
    static void setup() {
        List<TargetX_SRMb__Recommendation__c> recLists = new List<TargetX_SRMb__Recommendation__c> ();
        Contact con = new Contact();
        con.LastName = 'TestContact';
        insert con;
        TargetX_SRMb__Application__c app = new TargetX_SRMb__Application__c();
        app.TargetX_SRMb__Contact__c = con.Id;
        insert app;
        for (Integer i = 0; i < 2; i++ ) {
            TargetX_SRMb__Recommendation__c rec = new TargetX_SRMb__Recommendation__c();
            rec.Name = 'RecommendationTest'+i;
            rec.TargetX_SRMb__Email__c = 'test'+i+'@test.com';
            rec.TargetX_SRMb__Contact__c = con.Id;
            rec.TargetX_SRMb__Application__c = app.Id;
            recLists.add(rec);       ////  Need this line to add the records in recLists for insertion.
        }.
        insert recLists;
    }

   
Thanks,
Maharajan.C