5

I have a simple invocable method (thanks, Rakesh Gupta!) that I'm calling from a Process Builder when certain criteria are met. I'm getting good results from the code as evidenced by reviewing data in the UI, but I'm not sure how to write a test class for an @InvocableMethod.

Here is the invocable method:

public class AssignLeadsUsingAssignmentRules
 {
    @InvocableMethod
    public static void LeadAssign(List<Id> LeadIds)
    {
        Database.DMLOptions dmo = new Database.DMLOptions();
        dmo.assignmentRuleHeader.useDefaultRule= true;          
        Lead Leads=[select id from lead where lead.id in :LeadIds];
        Leads.setOptions(dmo);
        update Leads;
    }
 }

Here is test class that I've written:

@isTest
private class AssignLeadsUsingAssignmentRulesTest {

    private static testMethod void doTest() {

        Test.startTest();
        Lead l = new Lead(LastName = 'Test Lead',
                     Company = 'Test Company',
                     IsRecruitmentLead__c = True);
        insert l;
        Test.stopTest();

    }
}

The test class creates data that meets the criteria for the Process Builder, and the test class executes to completion in the Developer Console, but I'm still getting 0% code coverage for the invocable class.

Thanks in advance for your advice - Tom

2 Answers 2

9

Not sure if these methods get invoked automatically or not but you could invoke it directly in your test.....

Also, you need some asserts to make sure what you expect to happens does. Simply executing code is not a test at all...

@isTest
private class AssignLeadsUsingAssignmentRulesTest {

    private static testMethod void doTest() {

        Test.startTest();
        Lead l = new Lead(LastName = 'Test Lead',
                     Company = 'Test Company',
                     IsRecruitmentLead__c = True);
        insert l;

        AssignLeadsUsingAssignmentRules.leadAssign(New Lead[]{l});

        Test.stopTest();

        l = [Select OwnerID From Lead where ID = :l.id];
        system.assertEquals(WHATYOUEXPECT, l.OwnerID);

    }
}
4
  • Was just typing this answer myself...this is what I always do Commented Oct 16, 2015 at 17:05
  • Thanks guys ... I tried this just now. Got a successful test, but still no code coverage. Any other thoughts?
    – Tom Barad
    Commented Oct 16, 2015 at 17:26
  • Did it pass with the assert and is the assert not using the running user as the what you expect part. If so then you have coverage Where r u looking at the code coverage? Make sure u do not have only store aggregate code coverage checked
    – Eric
    Commented Oct 16, 2015 at 18:35
  • I was looking at the Developer Console "Overall Code Coverage" pane after running the test using the "Run Test" button. I just had a hunch and did a "Run All" tests and I got 100% coverage for this code. Thanks for all your help!
    – Tom Barad
    Commented Oct 16, 2015 at 20:42
0

Hopefully someone finds this helpful 7 years later. I struggled a bit with this conceptually especially since my process was fired off of an Asynchronous Flow that was populating data from the E2C Incoming Email Message to the Case that was created. The invocable itself was taking in and outputting a List<List> since I was using CMT and returning a number of values different from the input (yet the same due to the <List<List>). Edit: for some reason Stack Exchange won't process the syntax for s-t-r-i-n-g. But those are lists of list s-t-r-i-n-g

Anyways. What I realized was that I was trying to write a test class for the larger process as a whole which was...well...difficult. I took a breather and realized I just needed to validate the invocable and essentially just called my invocable inside of the test class. The main key was storing the returned data from the invocable as a List<List> variable again to be able to assert it against what I needed to.

Hopefully this helps someone.

1
  • 2
    The site is looking at your "List<List< String >>" (but without the extra spaces) and reading it as being formatted for something else (I think html). Thus that inner "<String>" is getting hidden. Try surrounding the whole "phrase" with backticks [ ` ] (usually found on the same key as the tilde [ ~ ] to the left of the [ 1 ] key), with no space between the backtick and "LIst" (without quotes), and no space between the backtick and the last ">", and see what that looks like.
    – Moonpie
    Commented Dec 7, 2022 at 14:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .