function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
goabhigogoabhigo 

Test class for Flow

Hi I am stuck in a point which I believe is almost 'the end', but I am not just able to write test class for this.

 

I have a flow, which shows a list of Opportunities, one by one based on score. This flow has been referenced in a VF page:

<apex:page controller="OppPriorityController">
    <h1>Opportunity Prioritisation Wokflow</h1>
  <flow:interview name="Opportunity_Prioritisation_Flow" interview="{!myflow}" buttonLocation="bottom">
  <apex:param name="varCurrentUserID" value="{!$User.Id}"/> 
  </flow:interview>
</apex:page>

 The OppPriorityController is below:

public class OppPriorityController {

    // Need not instantiate explicitly the Flow object using the class constructor 
    
    public Flow.Interview.Opportunity_Prioritisation_Flow myflow { get; set; }
    public String getPhoneNumber() {
        // Access flow variables as simple member variables with get/set methods 
             return myflow.PhoneNumber; 
             }
    public String getAccountID() {
        // Access flow variables as simple member variables with get/set methods 
             return myflow.AccId;
    }
     public String getAccountName() {
        // Access flow variables as simple member variables with get/set methods 
             return myflow.AccountName;
    }
     public String getOpportunityID() {
        // Access flow variables as simple member variables with get/set methods 
             return myflow.OppId;
    }
     public String getOpportunityName() {
        // Access flow variables as simple member variables with get/set methods 
             return myflow.OppName;
    }
}

 The test class which I tried is below:

@isTest (SeeAllData=true)
private class OppPriorityControllerTest {
    public static testMethod void myTestMethodForFlow() {
        PageReference pageRef = Page.OppPriorityPage;
        Test.setCurrentPage(pageRef);
        OppPriorityController oppPriorityController = new OppPriorityController();        
        oppPriorityController.myflow = new Flow.Interview.Opportunity_Prioritisation_Flow(new Map<String, Object>());
        String pNumber = oppPriorityController.getPhoneNumber();
        String accountId = oppPriorityController.getAccountID() ;
        String accountName = oppPriorityController.getAccountName();
        String opportunityID = oppPriorityController.getOpportunityID();
        String opportunityName = oppPriorityController.getOpportunityName();
    }
}

 I received an error: Interview not started.

 

Any help here would be grateful. I tried to serach in various places, but none helped me to solve this.

 

 

 

 

RajaramRajaram

Yes, this is a limitation as of now and something we are looking to support.

Haystack CertifiedHaystack Certified

I know this thread is little old, but hopefully I can get a clarification.  Based on not being able to instantiate a flow from Apex, it sounds like it is not possible to get 100% test coverage.  Is that correct?  Thanks.

craskulineczcraskulinecz

Is there any word on the best way to write  a test class for flow?

DarrellDDarrellD

You really cannot do this.  If you are calling the Flow from a VisualForce page, you can still call the VF page from the test class but it pretty much stops there.  You cannot call a Flow from a test class yet.  If the Flow is supposed to return a variable or similar then best you can do is kind of "fudge" the test class so the coverage succeeds but you really aren't "testing" anything.

 

I just opened a Developer case on this earlier this week and this was the solution the SalesForce Developer came up with to incresase my coverage.  I had a variable (AccountID) that was being returned from the Flow.  We couldn't test if it was the correct Account ID since we cannot call a Flow so all he did was create an Account in the test class and confirm that an Account was created.  So it was a "fudge" for now.

 

Darrell

Haystack CertifiedHaystack Certified

I did something like that as well and got to 90% coverage.  The trickiest part is any code that runs when the flow object is null cannot be tested, so you have to do what you can to move that code out of that branch.

craskulineczcraskulinecz

Darrell,

 

Can you give a code example of how to accomplish this?

 

Thanks.

Haystack CertifiedHaystack Certified

Here is a class with test method that gets 100% coverage, but only because varCreateCase is being tested on the same statement as the test for myFlow != null. That's probably not best practice. It also employ "testmode" as a cheat to force the if statement to be true for testing purposes.

public class CaseOptionFlowController {

    public Flow.Interview.Create_Case_Option myflow { get; set; }

    public PageReference getFinishScreen() {
        return getFinishScreen(false);
    }

    public PageReference getFinishScreen( boolean testmode ){
        String theURL = 'https://na12.salesforce.com/apex/CreateCaseOption?sfdc.tabName=01rU0000000Q08H';
        if((( myFlow != null ) && ( myflow.varCreateCase == true )) || ( testmode == true ) ) {
            theURL = 'https://na12.salesforce.com/500/e?retURL=%2F500%2Fo&RecordType=012U0000000LvOV&ent=Case';
        }    
        PageReference p = new PageReference(theURL);
        p.setRedirect(true);

        return p;
    }

    static testmethod void testController() {
        CaseOptionFlowController x = new CaseOptionFlowController();
        PageReference urlpageref0 = x.getFinishScreen();
        PageReference urlpageref1 = x.getFinishScreen(true);
    }
}

 

DarrellDDarrellD

So it looks like the 2 examples posted are similar to what I was saying.  As you see both reference "tricking", or as I said "fudging" the test class to increase the coverage though you aren't really testing some parts of the class.

Colin KenworthyColin Kenworthy
I'm not sure if this method was around in 2012 but you can run your flows in test classes with .start()
 
@isTest (SeeAllData=true)
private class OppPriorityControllerTest {
    public static testMethod void myTestMethodForFlow() {
        PageReference pageRef = Page.OppPriorityPage;
        Test.setCurrentPage(pageRef);
        OppPriorityController oppPriorityController = new OppPriorityController();       
        oppPriorityController.myflow = new Flow.Interview.Opportunity_Prioritisation_Flow(new Map<String, Object>());
        oppPriorityController.myflow.start();
        String pNumber = oppPriorityController.getPhoneNumber();
        String accountId = oppPriorityController.getAccountID() ;
        String accountName = oppPriorityController.getAccountName();
        String opportunityID = oppPriorityController.getOpportunityID();
        String opportunityName = oppPriorityController.getOpportunityName();
    }
}

See the extra line 08.
Theresa MarshTheresa Marsh
Unfortunately, the start method isn't usable if the flow has any screens/requires user input.  You'll get the error: "Start can only be called on an autolaunched flow".  

I got the 'Interview not started" error even with the myflow != null check

 
Mark Tyler CrawfordMark Tyler Crawford
Is this still a testing limitation for Flows?
Dilip Reddy PDilip Reddy P
Hi Kindly let me know if the limitation is still not sorted or if sorted guide me further