2

I've got an autolaunched flow called Sales_Rep_Suggester that contains 2 input-enabled variables: 1 is a string and the other is a Boolean. It also has 1 User-typed output-enabled variable.

    <variables>
        <description>Where does the person needing service live?</description>
        <name>Country_of_Residence</name>
        <dataType>Picklist</dataType>
        <isCollection>false</isCollection>
        <isInput>true</isInput>
        <isOutput>false</isOutput>
    </variables>
    <variables>
        <description>Is this a first-time customer?  (Leave unchecked for returning customers.)</description>
        <name>First_Time_Customer</name>
        <dataType>Boolean</dataType>
        <isCollection>false</isCollection>
        <isInput>true</isInput>
        <isOutput>false</isOutput>
        <value>
            <booleanValue>false</booleanValue>
        </value>
    </variables>
    <variables>
        <description>The user record representing the sales rep our customer needs to work with</description>
        <name>Sales_Rep_User</name>
        <dataType>SObject</dataType>
        <isCollection>false</isCollection>
        <isInput>false</isInput>
        <isOutput>true</isOutput>
        <objectType>User</objectType>
    </variables>

I'd like to write an Apex unit test validating that its branches work as expected -- that certain combinations of values for the inputs store a value into the output.

I can't figure out how. This is what I thought various instructions on the internet were saying I should do, but it won't compile:

@isTest
private class Sales_Rep_Suggester_Flow_TEST {
    private static testMethod void first_time_japan() {
        //Flow.Interview flw = new Flow.Interview.Sales_Rep_Suggester(); // Compile error:  "Constructor not defined: [Flow.Interview.Sales_Rep_Suggester].()"
        Test.startTest();
        //flw.start();
        Test.stopTest();
    }
}
1
  • Generally, you should not unit test declarative approaches. If you feel the need to protect the functionality with automated testing, it is a strong indicator the logic itself belongs in code.
    – Adrian Larson
    Oct 24, 2022 at 16:56

1 Answer 1

6

You have to provide the inputs as a parameter:

Map<String, Object> inputs = new Map<String, Object> {
  'First_Time_Customer' => true,
  'Country_of_Residence' => 'US'
};
Flow.Interview flw = new Flow.Interview.Sales_Rep_Suggester(inputs);
Test.startTest();
flw.start();
Test.stopTest();
sObject resultUser = (User)flw.getVariableValue('Sales_Rep_User');
// Do assertions here

You can read more in the official documentation.

2
  • Oh thanks, I could not turn up that particular doc link w/ my Google-fu for the life of me!
    – k..
    Oct 24, 2022 at 16:26
  • 1
    @k.. I have days like that, too, but this one I just happened to know where to look.
    – sfdcfox
    Oct 24, 2022 at 16:27

You must log in to answer this question.

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