salesforceapexapex-codesalesforce-communitiessalesforce-flow

Test Class for salesforce Invocable method


Recently, one of our customer has updated their salesforce which caused me to have to remake apex code that we had for an automated process. I was able to rebuild all of the code but the problem I am running into is testing the apex classes to get the necessary code coverage in order to push it to production. I am able to get the tests to run but they are only getting 46% code coverage.

This code is called by a flow which is how the variables are really going to be passed in production. When the code is called by the flow, all data is passed correctly and it works exactly how it should with 0 errors. The last part is just getting to code coverage so I am able to push it to production. After looking at the logs I determined that the variables that are needed for the code to run are not being passed.

There are three total classes, the test class, 1 invocable class and 1 class that is called from the invocable method.

This is the test class:

@isTest(SeeAllData = true)
public class TestMethods {
    private static testMethod void doTest() {
        Test.startTest();

        List<ProcessHandlerShowAccounts.DonationParameters> params = new List<ProcessHandlerShowAccounts.DonationParameters>();       
        ProcessHandlerShowAccounts.getDonorInfo(params);

        Test.stopTest();
    }
}

Invocable Method Class:

public class ProcessHandlerShowAccounts {
    public class DonationParameters {
        @InvocableVariable(required=true)
        public String account_id;
        @InvocableVariable(required=true)
        public String donation_id; 
        @InvocableVariable(required=true)
        public String contact_id;
    }
    @InvocableMethod(label='getDonorInfo' description='I hope this will work' category='AccountCategory')
    public static void getDonorInfo(DonationParameters[] donationParameters) {
        Account[] accounts = new Account[]{};
        system.debug('right before we call class ' + donationParameters);
        ID jobID = System.enqueueJob(new NewDonorLetter2(donationParameters));
    }
}

(Currently the last class does not matter but I will provide it if necessary)

After looking at the logs and locating the system debug in the getDonorInfo method, the "donationParameters" variable is null:

13:42:34:005 USER_DEBUG [13]|DEBUG|right before we call class: ()

I tried this with no luck:

@InvocableVariable(required=true)
public String account_id = "000000000";
@InvocableVariable(required=true)
public String donation_id = "000000000"; 
@InvocableVariable(required=true)
public String contact_id = "000000000";

Any help would be greatly appreciated!


Solution

  • You're passing an empty list into your invocable method in the test. Add an instance of DonationParameters to your params list before calling the invocable. Your debug statement shows an empty array (not exactly null, but no items).

    Some other notes: