salesforceapextest-class

Test Class for Invocable Method (Salesforce Apex) failing (not sure if covering 100%)


I successfully created an invocable method that I can call from a Salesforce flow to query the Contact Object and return a set of Ids with a passed in (invocable) variable (in this case an eMail address).

public class askFSIS_Match_eMail_ContactIds {
    @InvocableMethod(label='Find Unique Matching eMail Contact Ids'
                    description='Find potential contacts using eMail address as matching field and return a set of Contact Ids.')
    
    public static List<List<String>> getEmailContactIds (List<emIdRequest> emidreq)
    {
        List<Contact> eMailContacts = new List<Contact>();
        
        if(emidreq[0].eMailAddress != null)
        {
            String eMaddress = emidreq[0].eMailAddress;

            eMailContacts = [SELECT Id
                             FROM Contact
                             WHERE Email = :eMaddress
                             AND Inactive__c = false
                             AND Is_Deleted__c = false
                             AND IsDeleted = false
                             AND askFSIS_Contact_Type__c != 'askFSIS Internal'];
        }
            
        Set<Id> eMailContactIdSet = (new Map<Id,Contact>(eMailContacts)).keySet();
        List<Id> eMailContactIds = new List<Id>(eMailContactIdSet);
        
        List<List<Id>> eMailContactIdList = new List<List<Id>>();
        eMailContactIdList.add(eMailContactIds);

        Return eMailContactIdList;
    }    
    
    public class emIdRequest
    {
        @InvocableVariable(label='eMail Address' required=true)
        public String eMailAddress;
    }
}

Although, I could never figure out how to return it with a named variable, it works.

My issues is with creating the Test Class. The code compiles without problems but fails when running the test (log indicates "List index out of bounds: 0"), so I can't even determine the coverage ...

@isTest
public class askFSIS_Match_eMail_ContactIds_Test {
    static testMethod void testGetEmailContactIds() {

    Contact con = new Contact();
    con.FirstName = 'Jack';
    con.LastName = 'Jill';
    con.eMail = 'jack@jill.invalid';
    insert con;

    List<askFSIS_Match_eMail_ContactIds.emIdRequest> inputparams = new List<askFSIS_Match_eMail_ContactIds.emIdRequest>();
    askFSIS_Match_eMail_ContactIds.emIdRequest pf = new askFSIS_Match_eMail_ContactIds.emIdRequest();
    pf.eMailAddress = 'jack@jill.invalid';
        
    Test.startTest();
    
        List<List<Id>> contact_jack = askFSIS_Match_eMail_ContactIds.getEmailContactIds(inputparams);
        
    Test.stopTest();
    
    System.assert (contact_jack != null);
        
    }
}

I was, of course HOPING it would run (pass) and give me 100% coverage, but I am stuck. Admittedly, I am an absolute newbie to Apex programming (while not to programming, in general).

Any help would be greatly appreciated!


Solution

  • The problem is in this line of code:

    if(emidreq[0].eMailAddress != null)
    

    That happens because you are passing empty emidreq list to the method. Which leads us to the conclusion that you have forgotten to add test askFSIS_Match_eMail_ContactIds.emIdRequest to the list.

    To solve this, in the test class, after these lines:

    List<askFSIS_Match_eMail_ContactIds.emIdRequest> inputparams = new List<askFSIS_Match_eMail_ContactIds.emIdRequest>();
    askFSIS_Match_eMail_ContactIds.emIdRequest pf = new askFSIS_Match_eMail_ContactIds.emIdRequest();
    pf.eMailAddress = 'jack@jill.invalid';
    

    Add this line of code:

    inputparams.add(pf);