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 

Testing Save and New Extension

Hey there,

I need a small hand with testing the new bit of code that I have added to my extension. I feel like I am one line of code away from passing, i have just never tested for a save and new, so I am not sure on how to do this. The extension already saves, cancels and clones (and passes 100%). The save and new fucntion was jsut added and I was hoping I could get a small hand in testing for it. The newly added bits will be 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 saveNewDestinyService(){
   
   
        insert ser;
 
        // Send the user to the detail page for the new account.
       PageReference pageRef= new PageReference('/a0I/e');
       
        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;
   
    }
   
  

}



test:

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);
        ApexPages.currentPage().getParameters().put('clone', '1');

//now call the class and reference the standardcontroller in the class.
        extSaveSerButton extSer = new extSaveSerButton(stdSer);

//call the pageReference in the class.
        extSer.saveDestinyService();
        extSer.saveNewDestinyService();
        extser.cancelDestinyService();



Thank you for your time.
Best Answer chosen by Developer.mikie.Apex.Student
Rahul SharmaRahul Sharma
Testing through test class is very similar to how we test the page actually.
After calling saveDestinyService() function, your page has already been redirected to a new page.
So you would need to load the data controller class again before calling the saveNewDestinyService() method.

It is a good practice to add Test.startTest() and Test.stopTest() in your test method.

Let me know if you need more help. Also this question should have been went in Apex section.

Thanks,
Rahul

All Answers

Rahul SharmaRahul Sharma
Testing through test class is very similar to how we test the page actually.
After calling saveDestinyService() function, your page has already been redirected to a new page.
So you would need to load the data controller class again before calling the saveNewDestinyService() method.

It is a good practice to add Test.startTest() and Test.stopTest() in your test method.

Let me know if you need more help. Also this question should have been went in Apex section.

Thanks,
Rahul

This was selected as the best answer
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
Hey Rahul, 

Thanks for the reply. I Do use the start and stoptest tags, except this is a portion of my test which tests all my extensions similar to this(I have 5 objects which all have similar extensions). 

How would I go about loading the data controller class again in my test method?

Thank you so much for your help
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
Hey Rahul,

I did it and it passed 100% :). I will post my code, please tell me if there is anything I can simplify or change to make it better.

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);
        service__c viceTest = new Service__c(Account__c = acc.id);
       
        //call the apepages stad controller
        Apexpages.Standardcontroller stdSer = new Apexpages.Standardcontroller(serTest);
        ApexPages.currentPage().getParameters().put('clone', '1');

//now call the class and reference the standardcontroller in the class.
        extSaveSerButton extSer = new extSaveSerButton(stdSer);

//call the pageReference in the class.
        extSer.saveDestinyService();
        extser.cancelDestinyService();
       
         Apexpages.Standardcontroller stdvice = new Apexpages.Standardcontroller(viceTest);
         extSaveSerButton extSer2 = new extSaveSerButton (stdvice);
         extSer2.saveNewDestinyService();
Rahul SharmaRahul Sharma
This seems perfect!
One more very good approach would be to write different unit test methods for each scenarios.

Thanks,
Rahul