You need to sign in to do that
Don't have an account?
Julianne 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.
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.
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
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.