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
Julianne BoleyJulianne Boley 

Test Class for Controller Extension that sets the Finish Page for a Force.com Flow

I am not an advance developer by any definition, and I have been trying to figure this out pouring over other blogs and forums for days now.  I finally give up and am hoping someone can help me.

I have a flow that I am calling and passing variables to via a custom button (Visualforce Page) on the Account detail page.  The page setting the flow finish page and redirecting the user to the record created by the flow by using a controller extension.  

Everything is working as it should, but I won't be able to move any of this to production until I can figure out a way to get test coverage of the controller extension.

This is the class:
public class takeMetoContractController {
public takeMetoContractController(ApexPages.StandardController controller) {
}

public Flow.Interview.Buan_Test rFlow {get; set;} 
   

    public String getcontractID() {

        if (rFlow==null) return '';

        else return rFlow.varMcContractID;

    }


    public PageReference getFinishPage(){

        PageReference p = new PageReference('/' + getcontractID() );

        p.setRedirect(true);

        return p;
    }
}


It is being used in this page:
<apex:page sidebar="false"  standardController="Account" extensions="takeMetoContractController">

<flow:interview name="Buan_Test" interview="{!rFlow}" finishLocation="{!FinishPage}">
<apex:param name="Accountsfid" value="{!account.Id}"/>
<apex:param name="AccountSFName" value="{!account.Name}"/>
</flow:interview>
</apex:page>

 
I've got no idea how to get this test class to actually call the controller extension appropriately, but I have managed to get a test class to save.  Here is what I have (no making fun, please):

@isTest
public class testtakeMetoContractController {

public static void testtakeMetoContractController() {

    //Insert Account
    Account acct = new Account(Name='TestClassAccount');
    insert acct;
   
    // Retrieve the new Account
    acct = [SELECT Id,Name FROM Account WHERE Id =:acct.Id];
   
    //Insert Contact
    Contact c = new Contact (Account=acct, FirstName='Test', LastName='Contact');
    insert c;
   
    //Insert McContract
    McContracts__c mc = new McContracts__c (Account_Name__c=acct.Id, Contact__c=c.Id);
    insert mc;
   
    //Retrieve the new Contract
    mc = [Select Id, Name FROM McContracts__c WHERE Id =:mc.Id];
   
    String mcID = mc.Id;
    System.debug(mcID);

    ApexPages.StandardController sc = new ApexPages.StandardController(acct);
    takeMetoContractController ec = new takeMetoContractController(sc);
   
    PageReference FlowContractPageRef = Page.FlowContract;
    Test.setCurrentPage(FlowContractPageRef);

    string vaMcContractId=mcId;
    ec.getFinishPage();
    ec.getContractId();
   
    System.test.startTest();
    System.debug('ContractId - Add ContractId');
    System.debug('ContractId - Save');
    System.test.stopTest();
     
}
}


It is clearly a mess, and I really have no idea what I am doing, so if anyone has a solution to this or feels like being a good samaritan and wants to help, it would be greatly appreciated.
Michael VerhovskiMichael Verhovski
Julianne,  An Introduction to Apex Code Test Methods (https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods) and How to Write Good Unit Tests (https://developer.salesforce.com/page/How_to_Write_Good_Unit_Tests) document should help you to start 

As for you code, you should use isTest annotation on test methods or an equivalent to the testMethod keyword. 
Your unit test should 
  • Set up all conditions for testing.
  • Call the method being tested.
  • Verify that the results are correct.
You are doing the first two, but not the last one. Look for use of  System.assert() methods, instead of System.debug.

And do not forget to check the code coverage
Julianne BoleyJulianne Boley
Thanks, Michael.  I think I was a little unclear in my post.  I have written test classes with 100% code coverage before.  I just don't know how to write this particular test class because there is no way to instantiate a flow from a test, and I'm not sure that I am recreating what is necessary to call the extension.  I have zero code coverage with the current test class which leads me to believe that I am not calling the method that is being tested at all.  I'll swap out the debug statements for asserts, but I'm pretty sure that isn't going to give me any coverage if I'm not getting any at all right now, right?
Jorge Aquino @ AscentERPJorge Aquino @ AscentERP
Julianne, wondering if you were able to resolve the code coverage issue and how, as I am experiencing the same problem. I have searched and found many developers asking the same question but nobody offering any solutions.  Thanks.
Julianne0223Julianne0223
Jorge,
I was able to get it to work.  Here is the class, page, and test class that worked for me.  You'll need to tweak it for your own use case:

Controller:
public class takeMetoContractController {
public takeMetoContractController(ApexPages.StandardController controller) {
}

 public Flow.Interview.McCormick_Contracts_Data_Collection_2 rFlow {get; set;}  
    

    public String getcontractID() {

        if (rFlow==null) return '';

        else return rFlow.McContractid;

    }


    public PageReference getFinishPage(){

        PageReference p = new PageReference('/' + getcontractID() );

        p.setRedirect(true);

        return p;
    }


}

Test Class:
@isTest
public class testtakeMetoContractController {

public static testmethod void testtakeMetoContractController() {

    //Insert Account
    Account acct = new Account(Name='TestClassAccount');
    insert acct;
    
    // Retrieve the new Account
    acct = [SELECT Id,Name FROM Account WHERE Id =:acct.Id];
    
    //Insert Contact
    Contact c = new Contact (Account=acct, FirstName='Test', LastName='Contact');
    insert c;
    
    //Insert McContract
    McContracts__c mc = new McContracts__c (Account_Name__c=acct.Id, Contact__c=c.Id);
    insert mc;
    
    //Retrieve the new Contract
    mc = [Select Id, Name FROM McContracts__c WHERE Id =:mc.Id];
    
    String mcID = mc.Id;
    System.debug(mcID);

    ApexPages.StandardController sc = new ApexPages.StandardController(acct);
    takeMetoContractController ec = new takeMetoContractController(sc);
    
    PageReference FlowContractPageRef = Page.FlowContract;
    Test.setCurrentPage(FlowContractPageRef);

    string McContractid=mcId;
    ec.getFinishPage();
    ec.getContractId();
    
    System.test.startTest();
    System.debug('ContractId - Add ContractId'); 
    System.debug('ContractId - Save');
    System.test.stopTest();
   
    
}
}

VF Page:

<apex:page sidebar="false" standardController="Account" extensions="takeMetoContractController">
<flow:interview name="McCormick_Contracts_Data_Collection_2" interview="{!rFlow}" finishLocation="{!FinishPage}">
<apex:param name="Accountsfid" value="{!account.Id}"/>
<apex:param name="AccountSFName" value="{!account.Name}"/>
</flow:interview>
</apex:page>


I can't remember exactly what was blocking the test from working before, but I think it had something to do with a setting on a field in the flow that was being used.  I hope this helps.  Let me know if you have any questions.
Jorge Aquino @ AscentERPJorge Aquino @ AscentERP
thank you.
Eric F-NEric F-N
Thanks Julianne! I was struggling with the exact same issue and this resolved it for me too!