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
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student 

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
Best Answer chosen by Developer.mikie.Apex.Student
Phillip SouthernPhillip Southern
I think the clone piece is what needs to be tested.  So you'll need a test method calling the save method...create the page, set to current page, set the controller, put parameters in the page, then call the controller methods.



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

Phillip SouthernPhillip Southern
Hi, you can call multiple pagereferences in the same test class.  So just add a line after you save pagereference call:

extser.cancelDestinyService();
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
Thanks so much mate, I now have 92% code coverage rather than 64%. i will select your response as best answer. But i was just wondering if you could look at the code and see how I may be able to get 100%?
Phillip SouthernPhillip Southern
I think the clone piece is what needs to be tested.  So you'll need a test method calling the save method...create the page, set to current page, set the controller, put parameters in the page, then call the controller methods.



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 selected as the best answer
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
Am I able to simply add this to my massive test class that is testing everything?
Phillip SouthernPhillip Southern
Actually yes I think you can.
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
Thank you soo much. You are an absolute legend mate. 100% coverage.

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();
       


}
Phillip SouthernPhillip Southern
No problem!  Glad I could help!