You need to sign in to do that
Don't have an account?
goabhigo
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.
Yes, this is a limitation as of now and something we are looking to support.
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.
Is there any word on the best way to write a test class for flow?
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
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.
Darrell,
Can you give a code example of how to accomplish this?
Thanks.
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.
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.
See the extra line 08.
I got the 'Interview not started" error even with the myflow != null check