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
Stephane Ott-HauvilleStephane Ott-Hauville 

How to build a test class for a visualforce page controller that references a flow?

Hello,

I am new to Apex and I built a page controller for a page that calls a flow and then grabs a variable from the flow to set a redirect page to that record (an Opportunity in this case). My problem is that I don't know how to write the test class for this code as I would need to reference the flow. The functionality works great but without the test class I can't move my changes to production. Any help is greatly appreciated.

The visualforce page code is this:
<apex:page sidebar="false" Controller="OpportunityRedirect" > <flow:interview name="LookupReturnOpp" interview="{!LookupReturnOpp}" finishLocation="{!prFinishLocation}"> <apex:param name="varCurrentUserID" value="{!$User.Id}"/> </flow:interview> </apex:page>

and the Apex Controller is this:
public class OpportunityRedirect{

    public String Account { get; set; }
// Instanciate the Flow for use by the Controller - linked by VF "interview" attribute
 public Flow.Interview.LookupReturnOpp LookupReturnOpp {get;set;}

 // Factor your PageReference as a full GET/SET
 public PageReference prFinishLocation {
 get { 
 PageReference prRef = new PageReference('/' + strOutputVariable);
 prRef.setRedirect(true);
 return prRef;
 }
 set { prFinishLocation = value; }
 }

 // Factor your Flow output variable pull as a full GET / SET
 public String strOutputVariable {
 get {
 String strTemp = '';

 if(LookupReturnOpp != null) {
 strTemp = string.valueOf(LookupReturnOpp.getVariableValue('varOppID'));
 }

 return strTemp;
 }

 set { strOutputVariable = value; }
 } 

}

The flow that is referenced is a screen flow that simply looks up an Opportunity and saves it's ID in the flow variable varOppID.
Best Answer chosen by Stephane Ott-Hauville
Stephane Ott-HauvilleStephane Ott-Hauville
Okay, I figured it out following the example here: https://salesforce.stackexchange.com/questions/193820/test-coverage-with-flow-and-finishlocation/193824

If anyone has the same question, here is the test code. It currently only runs the code and not actually tests anything but I will edit it later. Thank you Jayant for your answer!
 
@isTest
public with sharing class OpportunityRedirectTest {
    public static testMethod void test1() {
        OpportunityRedirect OppR = new OpportunityRedirect();
        pageReference p = OppR.prFinishLocation;
        String A = OppR.Account;
        String S = '/Account';
        pageReference p2 = new pageReference(S);
        OppR.prFinishLocation = p2;
        OppR.strOutputVariable = S;
    }
}

 

All Answers

Jayant DasJayant Das
You will need to write a test class to cover your Controller and not the VF page. Refer to this link (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm) for getting started on how to write test class for custom controllers.
Stephane Ott-HauvilleStephane Ott-Hauville
Okay, I figured it out following the example here: https://salesforce.stackexchange.com/questions/193820/test-coverage-with-flow-and-finishlocation/193824

If anyone has the same question, here is the test code. It currently only runs the code and not actually tests anything but I will edit it later. Thank you Jayant for your answer!
 
@isTest
public with sharing class OpportunityRedirectTest {
    public static testMethod void test1() {
        OpportunityRedirect OppR = new OpportunityRedirect();
        pageReference p = OppR.prFinishLocation;
        String A = OppR.Account;
        String S = '/Account';
        pageReference p2 = new pageReference(S);
        OppR.prFinishLocation = p2;
        OppR.strOutputVariable = S;
    }
}

 
This was selected as the best answer