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
Richard Houston 20Richard Houston 20 

Custom controller extension test class

Hi,

I have a custom controller for a visualforce page that contains a flow. I'm not sure where to even start to write a test class for it. Any help would be most appreciated! 

What I have is a custom object, Project Information, that has a record created through the flow. The exit page is then set to the newly created record's page. ProjInfoID is a variable within the flow. 
public class ProjInfoFlowController {

    public Flow.Interview.CreateProjInfo myFlow { get; set; }
    
    public String getmyID() {
        if (myFlow==null) return '';
        else return myFlow.ProjInfoID;
        }
    
    public PageReference getOID(){
        PageReference p = new PageReference('/' + getmyID());
        p.setRedirect(true);
        return p;
        }

}

 
Best Answer chosen by Richard Houston 20
ClintLeeClintLee
Create a new Apex Class with the name Test_ProjInfoFlowController.
 
@istest
public with sharing class Test_ProjInfoFlowController
{
    /**
     *  Test getmyID() method with null flow.
     *
    **/
    static testmethod void testOne()
    {
        Test.startTest();
        
        ProjInfoFlowController controller = new ProjInfoFlowController();
        String myId = controller.getmyID();
        System.assertEquals('', myId);        

        Test.stopTest();
    }

    /**
     *  Test getOID() method.
     *
    **/
    static testmethod void testTwo()
    {
        Test.startTest();
        
        ProjInfoFlowController controller = new ProjInfoFlowController();
        String returnPage = controller.getOID().getUrl();
        System.assert(returnPage.contains('/' + controller.getmyID());

        Test.stopTest();
    }
}

You'll also want to create a test method to test the scenario where the Flow is populated.  However, you'll need to post your VF page b/c from your controller it's not possible to tell what type of parameters the Flow requires.

Hope that helps,

Clint
 

All Answers

ClintLeeClintLee
Create a new Apex Class with the name Test_ProjInfoFlowController.
 
@istest
public with sharing class Test_ProjInfoFlowController
{
    /**
     *  Test getmyID() method with null flow.
     *
    **/
    static testmethod void testOne()
    {
        Test.startTest();
        
        ProjInfoFlowController controller = new ProjInfoFlowController();
        String myId = controller.getmyID();
        System.assertEquals('', myId);        

        Test.stopTest();
    }

    /**
     *  Test getOID() method.
     *
    **/
    static testmethod void testTwo()
    {
        Test.startTest();
        
        ProjInfoFlowController controller = new ProjInfoFlowController();
        String returnPage = controller.getOID().getUrl();
        System.assert(returnPage.contains('/' + controller.getmyID());

        Test.stopTest();
    }
}

You'll also want to create a test method to test the scenario where the Flow is populated.  However, you'll need to post your VF page b/c from your controller it's not possible to tell what type of parameters the Flow requires.

Hope that helps,

Clint
 
This was selected as the best answer
Richard Houston 20Richard Houston 20
Clint,

Thanks for the quick reply and help! I'll give this a try now. 

Here's the visualforce page's code. 
<apex:page controller="ProjInfoFlowController" tabStyle="Opportunity">
<style type="text/css">
.FlowText {
  color: green;
}
.FlowCurrency{
  color: Blue;
}
.FlowRadio{
  line-height: 150%;
  fonto-size: 18px;
}
.FlowTextArea{
  Font-size: 18px;
}
.FlowContainer {
  line-height: 150%;
  padding: 10px 10px;
  width: 90%;
  background-color: #004785 ;
}
.FlowDropdown{
  line-height: 200%;
}
</style>

<flow:interview name="CreateProjInfo" interview="{!myflow}" finishLocation="{!OID}">
</flow:interview>
</apex:page>