How to write a flow test class? | Salesforce Flow Test Class Basics and Test coverages

Specifically speaking we don’t need to be writing a test class to deploy a flow to the production environment. But in the ‘bizzaro world’, that is every now-and-then we get to meet a client who wants us to deploy only using test classes.

How to write a flow test class?

Mind you ! We can do that using flow tests in a simpler manner but he prefers to make us write apex code, while having a ‘no-code’ policy for rest of the project requirements.

Anyway, lets jump right in.

1. Record triggered flow

Record Triggered flow

The above flow is a record triggered flow: triggered each type the WorkStep record is inserted.

@isTest
public class WorkStepStatusUpdateTest {
    public static testmethod void testErrData(){
        
        Product2 pd = New Product2();
        pd.Name = 'test pd';
        insert pd;
        
        Account a = New Account();
        insert a;
        Id rectypeid = Schema.SObjectType.WorkOrder.getRecordTypeInfosByName().get('Field').getRecordTypeId();
               
        WorkOrder testWo = New WorkOrder();
        insert testWo;
        
        WorkPlan wp = New WorkPlan();
        wp.Name = 'test';
        wp.ParentRecordId = testWo.Id;
        insert wp;
        
        WorkStep testWs = New WorkStep();
        testWs.Name = 'test';
        testWs.WorkPlanId = wp.Id;
        testWs.MobileStatus__c = 'New';
            insert testWs;
        
        WorkStep updateWs = [SELECT Status,MobileStatus__c,Id FROM WorkStep LIMIT 1 ];
        System.AssertEquals(updateWs.MobileStatus__c, updateWs.Status,'Updated status succcesfully');
        
    }

}

2. Autolaunched Flows

Lets take an example as below which requires certain parameters to be passed.

Test class for flows in salesforce

Now the below test class invoked the flow by calling it out particularly and passing specific characters.

//  Flow.Interview flw = new Flow.Interview.'Name of the flow'(inputs);
@isTest
public class ManageLicensePresentTest {
    
    public static testmethod void testMethod(){
        Account acct = New Account();
        insert acct;
        
        Contact contactData = TestDataFactory.createContacts(1,acct.Id)[0];
        insert contactData;
        
        Id rectypeid = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Draft').getRecordTypeId();
 		Case testCase = TestDataFactory.createCasessWithoutOrigin(1,contactData.Id,rectypeid)[0];
        insert testCase;
        
       
        Map<String, Object> inputs = new Map<String, Object> 
          {'varUserLicense' => true,
            'varCaseRecord'   => testCase };
              
         Flow.Interview flw = new Flow.Interview.ManageLicensePresent(inputs);
        
        Test.startTest();
         flw.start();
              System.assertEquals(whatever you want here);
        
         Test.stopTest();
    }
    
}

Now, we can run the tests usually in the developer console using the ‘Run Test’ at the top right corner.

Flow Test Coverages | Query flow test coverages in Developer Console

Use this query to check for all the elements covered by the test class.

SELECT Id, Elementname, FlowTestCoverageId, FlowTestCoverage.ApexTestClass.Name FROM FlowElementTestCoverage WHERE FlowVersionId = 'Id of the particular flow from URL' AND FlowTestCoverage.ApexTestClass.Name = 'Name of the test class'

Use this query to check for the number of elements covered and uncovered by the test class and particular test methods.

SELECT Id, ApexTestClassId, ApexTestClass.Name, TestMethodName, FlowVersionId, FlowVersion.FullName, FlowVersion.DefinitionId, NumElementsCovered, NumElementsNotCovered FROM FlowTestCoverage WHERE FlowVersionId = 'Id of the particular flow from URL' AND ApexTestClass.Name =  'Name of the test class'

Leave a comment

error: Content is protected !!