You need to sign in to do that
Don't have an account?
Adding cancel button Test
Hey there,
I have recently added a cancel button to a record creation page running with an extension. The extension currently is used for saving and cloning, I placed a cancel method into the extension, but I am not sure how to test for it. My test code tests every one of my custom object save and clone buttons, how do I add a test for a cancel method.
My extension with new method Added highlighted:
public class extSaveSerButton {
public Service__c ser {get;set;}
public extSaveSerButton(ApexPages.StandardController controller) {
this.ser = (Service__c)controller.getRecord();
}
Public PageReference saveDestinyService(){
if(System.currentPageReference().getParameters().get('clone') == '1'){
//Set record id to null
ser.Id = null;
}
insert ser;
// Send the user to the detail page for the new account.
PageReference pageRef= new PageReference('/apex/DestinyAccount?id='+ser.account__c+'&Sfdc.override=1');
pageRef.getParameters().put('tab','Destiny Services');
return pageRef;
}
Public PageReference cancelDestinyService(){
PageReference pageRef = new PageReference('/apex/DestinyAccount?id='+ser.account__c+'&Sfdc.override=1');
pageRef.getParameters().put('tab','Destiny Services');
return pageRef;
}
}
and my test class with relevent section highlighted:
@isTest
private class TestExtSaveButton {
static testMethod void TestExtSaveButton(){
//Based on your code and what you give me, Fact Finder is a child of the Account and when you //attach fact finder it needs to be reference into an account.
// create a test account that will be use to reference the Fact Finder Record
Account acc = new Account(Name = 'Test Account');
insert acc;
//Now lets create Fact Finder record that will be reference for the Standard Account
Fact_Finder__c facTest = new Fact_Finder__c(Account__c = acc.id);
//call the apepages stad controller
Apexpages.Standardcontroller stdFac = new Apexpages.Standardcontroller(facTest);
//now call the class and reference the standardcontroller in the class.
extSaveButton ext = new extSaveButton(stdFac);
//call the pageReference in the class.
ext.saveFactFinder();
}
static testMethod void TestExtSaveProButton(){
Account acc = new Account(Name = 'Test Pro Account');
insert acc;
//Now lets create Destiny Products record that will be reference for the Standard Account
Destiny_Products__c proTest = new Destiny_Products__c(Account__c = acc.id);
//call the apepages stad controller
Apexpages.Standardcontroller stdPro = new Apexpages.Standardcontroller(proTest);
//now call the class and reference the standardcontroller in the class.
extSaveProButton extPro = new extSaveProButton(stdPro);
//call the pageReference in the class.
extPro.saveDestinyProducts();
}
static testMethod void TestExtSaveSerButton(){
Account acc = new Account(Name = 'Test Ser Account');
insert acc;
//Now lets create Destiny Products record that will be reference for the Standard Account
Service__c SerTest = new Service__c(Account__c = acc.id);
//call the apepages stad controller
Apexpages.Standardcontroller stdSer = new Apexpages.Standardcontroller(serTest);
//now call the class and reference the standardcontroller in the class.
extSaveSerButton extSer = new extSaveSerButton(stdSer);
//call the pageReference in the class.
extSer.saveDestinyService();
}
static testMethod void TestExtSaveTraButton(){
Account acc = new Account(Name = 'Test Tra Account');
insert acc;
//Now lets create Destiny Products record that will be reference for the Standard Account
Transaction__c traTest = new Transaction__c(Account__c = acc.id, Date_of_Payment__c = date.today(), Amount__c = 0);
//call the apepages stad controller
Apexpages.Standardcontroller stdTra = new Apexpages.Standardcontroller(traTest);
//now call the class and reference the standardcontroller in the class.
extSaveTraButton extTra = new extSaveTraButton(stdTra);
//call the pageReference in the class.
extTra.saveDestinyTransaction();
}
static testMethod void TestExtSaveComButton(){
Account acc = new Account(Name = 'Test Com Account');
insert acc;
//Now lets create Destiny Products record that will be reference for the Standard Account
Notes__c comTest = new Notes__c(Account__c = acc.id);
//call the apepages stad controller
Apexpages.Standardcontroller stdCom = new Apexpages.Standardcontroller(comTest);
//now call the class and reference the standardcontroller in the class.
extSaveComButton extCom = new extSaveComButton(stdCom);
//call the pageReference in the class.
extCom.saveComplianceNotes();
}
}
Thank you in advanced for your help
I have recently added a cancel button to a record creation page running with an extension. The extension currently is used for saving and cloning, I placed a cancel method into the extension, but I am not sure how to test for it. My test code tests every one of my custom object save and clone buttons, how do I add a test for a cancel method.
My extension with new method Added highlighted:
public class extSaveSerButton {
public Service__c ser {get;set;}
public extSaveSerButton(ApexPages.StandardController controller) {
this.ser = (Service__c)controller.getRecord();
}
Public PageReference saveDestinyService(){
if(System.currentPageReference().getParameters().get('clone') == '1'){
//Set record id to null
ser.Id = null;
}
insert ser;
// Send the user to the detail page for the new account.
PageReference pageRef= new PageReference('/apex/DestinyAccount?id='+ser.account__c+'&Sfdc.override=1');
pageRef.getParameters().put('tab','Destiny Services');
return pageRef;
}
Public PageReference cancelDestinyService(){
PageReference pageRef = new PageReference('/apex/DestinyAccount?id='+ser.account__c+'&Sfdc.override=1');
pageRef.getParameters().put('tab','Destiny Services');
return pageRef;
}
}
and my test class with relevent section highlighted:
@isTest
private class TestExtSaveButton {
static testMethod void TestExtSaveButton(){
//Based on your code and what you give me, Fact Finder is a child of the Account and when you //attach fact finder it needs to be reference into an account.
// create a test account that will be use to reference the Fact Finder Record
Account acc = new Account(Name = 'Test Account');
insert acc;
//Now lets create Fact Finder record that will be reference for the Standard Account
Fact_Finder__c facTest = new Fact_Finder__c(Account__c = acc.id);
//call the apepages stad controller
Apexpages.Standardcontroller stdFac = new Apexpages.Standardcontroller(facTest);
//now call the class and reference the standardcontroller in the class.
extSaveButton ext = new extSaveButton(stdFac);
//call the pageReference in the class.
ext.saveFactFinder();
}
static testMethod void TestExtSaveProButton(){
Account acc = new Account(Name = 'Test Pro Account');
insert acc;
//Now lets create Destiny Products record that will be reference for the Standard Account
Destiny_Products__c proTest = new Destiny_Products__c(Account__c = acc.id);
//call the apepages stad controller
Apexpages.Standardcontroller stdPro = new Apexpages.Standardcontroller(proTest);
//now call the class and reference the standardcontroller in the class.
extSaveProButton extPro = new extSaveProButton(stdPro);
//call the pageReference in the class.
extPro.saveDestinyProducts();
}
static testMethod void TestExtSaveSerButton(){
Account acc = new Account(Name = 'Test Ser Account');
insert acc;
//Now lets create Destiny Products record that will be reference for the Standard Account
Service__c SerTest = new Service__c(Account__c = acc.id);
//call the apepages stad controller
Apexpages.Standardcontroller stdSer = new Apexpages.Standardcontroller(serTest);
//now call the class and reference the standardcontroller in the class.
extSaveSerButton extSer = new extSaveSerButton(stdSer);
//call the pageReference in the class.
extSer.saveDestinyService();
}
static testMethod void TestExtSaveTraButton(){
Account acc = new Account(Name = 'Test Tra Account');
insert acc;
//Now lets create Destiny Products record that will be reference for the Standard Account
Transaction__c traTest = new Transaction__c(Account__c = acc.id, Date_of_Payment__c = date.today(), Amount__c = 0);
//call the apepages stad controller
Apexpages.Standardcontroller stdTra = new Apexpages.Standardcontroller(traTest);
//now call the class and reference the standardcontroller in the class.
extSaveTraButton extTra = new extSaveTraButton(stdTra);
//call the pageReference in the class.
extTra.saveDestinyTransaction();
}
static testMethod void TestExtSaveComButton(){
Account acc = new Account(Name = 'Test Com Account');
insert acc;
//Now lets create Destiny Products record that will be reference for the Standard Account
Notes__c comTest = new Notes__c(Account__c = acc.id);
//call the apepages stad controller
Apexpages.Standardcontroller stdCom = new Apexpages.Standardcontroller(comTest);
//now call the class and reference the standardcontroller in the class.
extSaveComButton extCom = new extSaveComButton(stdCom);
//call the pageReference in the class.
extCom.saveComplianceNotes();
}
}
Thank you in advanced for your help
PageReference pageRef = Page.YOURVFPAGENAMEHERE;
Test.setCurrentPage(pageRef);
Apexpages.Standardcontroller stdTra = new Apexpages.Standardcontroller(YOURSTANDARDOBJECT);
ApexPages.currentPage().getParameters().put('clone', '1');
etc.....call save method.
All Answers
extser.cancelDestinyService();
PageReference pageRef = Page.YOURVFPAGENAMEHERE;
Test.setCurrentPage(pageRef);
Apexpages.Standardcontroller stdTra = new Apexpages.Standardcontroller(YOURSTANDARDOBJECT);
ApexPages.currentPage().getParameters().put('clone', '1');
etc.....call save method.
This was the finished static testmethod
static testMethod void TestExtCloneTraButton(){
Account acc = new Account(Name = 'Test Tra Account');
insert acc;
Transaction__c traTest = new Transaction__c(Account__c = acc.id, Date_of_Payment__c = date.today(), Amount__c = 0);
//call the apepages stad controller
Apexpages.Standardcontroller stdTra = new Apexpages.Standardcontroller(traTest);
ApexPages.currentPage().getParameters().put('clone', '1');
extSaveTraButton extTra = new extSaveTraButton(stdTra);
extTra.saveDestinyTransaction();
}