Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I need to create a test class to test an apex Controller/Extension I have created.

Functionality Objective: Embed flow in Visualforce page, so that the flow is started from a custom button on the standard task object, the flow marks the originating task as completed then takes the user to a screen input form pre-populated with information from the originating task. The user then updates additional information and a new Sales History (custom object) record is created. User is then taken to a page confirming the Sales History was successfully logged, and the user is routed to the new Sales History record when they click the finish button.

Visualforce page:

<apex:page standardController="Task" extensions="flowLSHfTaskContExt" showHeader="false" sidebar="false">
<flow:interview name="LogSalesHistoryFromTask" interview="{!historyFlow}" finishLocation="{!SalesHistoryPage}" >
   <apex:param name="TaskID" value="{!Task.Id}" />
 </flow:interview>

Apex Controller/Extension:

public class flowLSHfTaskContExt {
private final Task task;
public flowLSHfTaskContExt(ApexPages.StandardController stdController){}
public Flow.Interview.LogSalesHistoryFromTask historyFlow { get; set; }
public String getSalesHistoryID() {
    if (historyFlow==null) return '';
    else return historyFlow.SalesHistoryID;
    }
public PageReference getSalesHistoryPage(){
    return new PageReference('/' + getSalesHistoryID());
    }
}

I now need to create a test class to test the above controller/extension. Here's what I have so far:

@isTest
public class flowLSHfTaskContExtTest {
    @isTest
    public static void testflowLSHfTaskContExt() {
          PageReference pageRef = Page.SalesHistoryPage;
          Test.setCurrentPage(pageRef);

        flowLSHfTaskContExt controller = new flowLSHfTaskContExt(ApexPages.StandardController );
        String nextPage = controller.save().getUrl();

        // Add parameters to page URL
        ApexPages.currentPage().getParameters().put('SalesHistoryID');

        // Instantiate a new controller with all parameters in the page
        controller = new flowLSHfTaskContExt(); 
        controller.setSalesHistoryID('a0Xc0000003Zc4n');
        nextPage = controller.save().getUrl();
    }
}

I can't seem to get the test class to save, and am not sure if I am heading down the right path with this.

I updated my test class to create an account (required on final Sales History record), then a task with the account ID as the what ID, then call the standard task controller and the flows controller extension, but I am still getting errors, such as SalesHistoryPage does not exist.

I am not sure that this is the best approach for the test class, as I don't think you can start a flow from an apex class.

Ok... so the user is clicking a Custom Button (detail page button) on the task record, the content source is a Visualforce page, which kicks off a Flow and sets the Finish Location of the Flow to the newly created Sales History Record. During the Flow the user is taken to an Input Screen where they add details for the Sales History Record when the user clicks the Next Button in the flow, the Sales History Record is created, and the Controller Extension gets the SalesHistoryID and creates a a temporary Page Reference getSalesHistoryPage(), which returns a new PageReference [('/' + getSalesHistoryID())]. The user is then taken to another screen in the flow, letting them know that the Sales History record was successfully created. The VisualforcePage then sets the finishLocation for the flow as {!SalesHistoryPage} which the user is redirected to when they click the Finish Button in the flow.

I don't believe I need to test the flows creation of the SalesHistory record, wouldn't I just need to test the controller extensions ability to pass the SalesHistoryID to the finish location on the VF page?

Here is my updated class, everything that causes an error on save has been commented out.

@isTest
public class flowLSHfTaskContExtTest {
@isTest
public static void testflowLSHfTaskContExt() {
    //Insert Account
    Account testAccount = new Account(Name='TestClassTaskAccount');
    insert testAccount;

    // Retrieve the new Account
    testAccount = [SELECT Id FROM Account WHERE Id =:testAccount.Id];

    // Insert Task
    Task testTask= new Task(Subject='TestClassTask',Type='Task',KSI_Type__c='Discovery',WhatId=testAccount.Id);
    insert testTask;

    // Retrieve the new Task
    testTask = [SELECT Id FROM Task WHERE Id =:testTask.Id];

    ApexPages.StandardController stdController = new ApexPages.StandardController(testTask);
    flowLSHfTaskContExt controller = new flowLSHfTaskContExt(stdController);
    // public Flow.Interview.LogSalesHistoryFromTask historyFlow { get; set; }
    // nextPage = controller.save().getPageReference();
    // PageReference pageRef = Page.SalesHistoryPage;
    // Test.setCurrentPage(pageRef);


    // controller.getSalesHistoryID;

    // Add parameters to page URL
    // ApexPages.currentPage().getParameters().put('SalesHistoryID');
}
}

Am I going about this the wrong way? Maybe I don't need a test class, since it is related to a flow? It seems like this shouldn't be so difficult. I am trying to get this pushed to production and launched to our users. Any help would be appreciated!

share|improve this question
    
What error are you getting when you try to save? –  Daniel Hoechst May 16 at 22:28
    
The instantiation of the controller extension would normally look like this Task t = new Task(...); insert t; flowLSHfTaskContExt c = new flowLSHfTaskContExt(new ApexPages.StandardController(t)); so that is one thing to fix. –  Keith C May 16 at 22:33
add comment

1 Answer 1

To create an instance of the class, you first need to get a StandardController.

So your steps in the test will be:

  1. Create and insert a Task record.
  2. Create an instance of ApexPages.StandardController
  3. Create an instance of flowLSHTaskContExt

Task testTask = new testTask(); //Add the fields required for a Task here
insert testTask;
ApexPages.StandardController stdController = new ApexPages.StandardController(testTask);
flowLSHfTaskContExt controller = new flowLSHfTaskContExt(stdController);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.