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
JoshTonksJoshTonks 

Test class help with save and redirect extension

I was hoping someone could help me. I have a controller extension which saves the record and then redirects the person away from the page. This is for our customers fo a customer feedback this all works for me but I have no idea how i would write a test class for the extension as I have done test classes for other things before but this I cannot get my head around any guidance would be much appreciated. This is my extension below im using www.google.com as a placeholder as I havn't got the page built just yet for the redirect.
 
public with sharing class CSATExtension
{
   ApexPages.StandardController stdCtrl;
   // extension constructor
   public CSATExtension(ApexPages.StandardController std)
   {
      stdCtrl=std;
   }

   public PageReference save()
   {
      stdCtrl.save();
      PageReference pr = new PageReference('http://www.google..com/');
      pr.setRedirect(true);
      return pr;
   }
}

 
Best Answer chosen by JoshTonks
Khan AnasKhan Anas (Salesforce Developers) 
Hi Tonks,

I trust you are doing very well.

Below is the sample test class which I have tested in my org and it is working fine. 
@isTest
public class ExtensionTestClass {
    
    static testMethod void MyTest() {        
        Account myaccount = new Account (name='Test');
        insert myaccount;
        
        Apexpages.StandardController sc = new Apexpages.StandardController(myaccount);        
        ExtensionTestController ext = new ExtensionTestController(sc);  // ExtensionTestController is my Class name
        
        PageReference pg = ext.save();
        system.assertEquals('http://www.google.com/', pg.getUrl());
        system.assertEquals(true, pg.getRedirect());
        system.assertEquals(new Map < String, String >(), pg.getParameters());
        system.assertEquals(new Map < String, String >(), pg.getHeaders());    
    }
}


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in future.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Tonks,

I trust you are doing very well.

Below is the sample test class which I have tested in my org and it is working fine. 
@isTest
public class ExtensionTestClass {
    
    static testMethod void MyTest() {        
        Account myaccount = new Account (name='Test');
        insert myaccount;
        
        Apexpages.StandardController sc = new Apexpages.StandardController(myaccount);        
        ExtensionTestController ext = new ExtensionTestController(sc);  // ExtensionTestController is my Class name
        
        PageReference pg = ext.save();
        system.assertEquals('http://www.google.com/', pg.getUrl());
        system.assertEquals(true, pg.getRedirect());
        system.assertEquals(new Map < String, String >(), pg.getParameters());
        system.assertEquals(new Map < String, String >(), pg.getHeaders());    
    }
}


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in future.

Thanks and Regards,
Khan Anas
This was selected as the best answer
JoshTonksJoshTonks
That one has solved it for me. Thank you very much.