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
TechSteveTechSteve 

save redirect unit testing

Hi all, needed to have a page redirect on save and achieved it with

 

public PageReference saveandnotes() {
          stdCtrl.save(); // This takes care of the details for you.
          PageReference pn = Page.PN;
          pn.getParameters().put('cid',ApexPages.currentPage().getParameters().get('cid'));
          pn.getParameters().put('form','OTReview__c');
          pn.setRedirect(true);
       return pn;
    }  

Trying now to write the unit test for it and getting no were fast. Got 70% cover overal just need this I think. So far i tried this but get the error

Compile Error: Method does not exist or incorrect signature: controller.saveandnotes() with 

Public Static TestMethod void saveandnotes()
        {
            // this builds the constructor for the controller which links this to the correct object
            OTReview__c testrev = new OTReview__c(OTLims1__c = 'abc', OTDiag__c = 'notlob', OTCognition__c = 'wife & kids',
                OTMental__c = 'can talk', OTPhysical__c = 'make tea', OTSensation__c = '50mg', OTMed__c = 'asprin',
                OTSafe__c = 'Yes');
                Insert testrev;

            ApexPages.StandardController stdController = new ApexPages.StandardController(testrev);
            OTRevConedit ARed = new OTRevConedit(stdcontroller);
           
            ApexPages.CurrentPage().getParameters().put('cid', '003R000000Y7Gw4');
            ApexPages.CurrentPage().getParameters().put('form', 'OTReview__c');
            String nextPage = controller.saveandnotes().getUrl();

            system.assertEquals('/apex/PN?cid=003R000000Y7Gw4', nextPage);

        }

  Any help would by appreciated emmensely.

Thanks

 

Steve

Best Answer chosen by Admin (Salesforce Developers) 
Ispita_NavatarIspita_Navatar

Hi,

You haven’t posted the exact class code hence try to follow the following steps, it should help resolve your issue in the test method:-

  • You have to create a contact inside the test method and pass that id (id of the newly created contact), instead of passing the hardcoded value. Refer to the following:- 

OTReview__c testrev = new OTReview__c(OTLims1__c = 'abc', OTDiag__c = 'notlob', OTCognition__c = 'wife & kids', OTMental__c = 'can talk', OTPhysical__c = 'make tea', OTSensation__c = '50mg', OTMed__c = 'asprin', OTSafe__c='Yes');

Insert testrev;

Contact con=new contact(firstname='xyz',lastname='test1');

insert con;

ApexPages.StandardController stdController = new ApexPages.StandardController(testrev);           

OTRevConedit ARed = new OTRevConedit(stdcontroller);           

ApexPages.CurrentPage().getParameters().put('cid', +con.id);


  • Now create the instance of your class and call the method saveandnotes using the instance of  the class. Make few changes accordingly. 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

All Answers

Ispita_NavatarIspita_Navatar

Hi,

You haven’t posted the exact class code hence try to follow the following steps, it should help resolve your issue in the test method:-

  • You have to create a contact inside the test method and pass that id (id of the newly created contact), instead of passing the hardcoded value. Refer to the following:- 

OTReview__c testrev = new OTReview__c(OTLims1__c = 'abc', OTDiag__c = 'notlob', OTCognition__c = 'wife & kids', OTMental__c = 'can talk', OTPhysical__c = 'make tea', OTSensation__c = '50mg', OTMed__c = 'asprin', OTSafe__c='Yes');

Insert testrev;

Contact con=new contact(firstname='xyz',lastname='test1');

insert con;

ApexPages.StandardController stdController = new ApexPages.StandardController(testrev);           

OTRevConedit ARed = new OTRevConedit(stdcontroller);           

ApexPages.CurrentPage().getParameters().put('cid', +con.id);


  • Now create the instance of your class and call the method saveandnotes using the instance of  the class. Make few changes accordingly. 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

This was selected as the best answer
TechSteveTechSteve

Hi Ispita_Navatar, I will give you the full code as I think I have covered you suggustion elsewhere,

** My controller extension **

public class AnnRevCon2edit {

    public String patid { get; set; }
   
    public String conid { get; set; }

    public final INS_Annual_Review__c ar;
  
    ApexPages.StandardController stdCtrl;
   
    public AnnRevCon2edit(ApexPages.StandardController stdController) {
        stdCtrl=stdController;
        this.ar = (INS_Annual_Review__c)stdController.getRecord(); // gets single records from this page.
        ar.ClientID__c = ApexPages.currentPage().getParameters().get('cid'); // assign's cid to ClientID__c
    }
  
    // puts Client info on screen **** working!
    public String Id  { get; private set; } 
 
    // temporary initialisation of contact controller to allow call to info held there.
    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
                patid = ApexPages.currentPage().getParameters().get('cid'); // loads cid value into patid
                    if(ApexPages.currentPage().getParameters().get('cid')!= null){     
                        conid = '?cid='+ApexPages.currentPage().getParameters().get('cid');
                    }
                Id id = ApexPages.currentPage().getParameters().get('cid');
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(              
                      [select name, Client_DOB__c, Contact_Ref__c, DNR_Authorisation_Uploaded__c from Contact where Id = :id]));
            }
            return setCon;
        }
        set;
    }

    // Initialize setCon and return a list of records
   
    public List<Contact> getContacts() {
         return (List<Contact>) setCon.getRecords();
    }
    // puts Client info on screen **** working

    //extending the save
      public PageReference saveandnotes() {
          stdCtrl.save(); // This takes care of the details for you.
          PageReference pr = Page.PN;
          pr.getParameters().put('cid',ApexPages.currentPage().getParameters().get('cid'));
          pr.getParameters().put('form','Annual_Review2__c');
          pr.setRedirect(true);
        return pr;
    } 
             
}

**My full test code **

@isTest
private class AnnRevCon2editTest
{
        static PageReference pageRef;
        static String isString {get; set; }
   
    Public static TestMethod void AnnRevCon2editTest()
        {
            pageRef = new PageReference('Annual_Review2e'); // used to set a page         
            test.setCurrentPage(pageRef);  // set starting page
            ApexPages.CurrentPage().getParameters().put('cid', '003R000000Y7Gw4');
                       
            // this builds the constructor for the controller which links this to the correct object
            INS_Annual_Review__c testrev = new INS_Annual_Review__c(ClientID__c = '003R000000Y7Gw4',
                Any_Other_info__c = 'djshfgjdh',Communication__c = 'abc',Sup_Net__c = 'sgdsfg',Functional__c = 'Yes',
                Falls__c = True,Past_History__c = 'sdfgksjf',PC_ra__c = True,OT__c = True,
                Med_Dose__c = 'vcxv',invOther__c = False,Med_Frequency__c = 'ageragearg',
                GTS__c = False, Med_Name__c = 'dgfgdfg');
                Insert testrev;

            ApexPages.StandardController stdController = new ApexPages.StandardController(testrev);
            AnnRevCon2edit AR = new AnnRevCon2edit(stdcontroller);
           
            List<INS_Annual_Review__c> Reviews = new List<INS_Annual_Review__c>{};
            for (Integer i = 0; i < 10; i++) {
                INS_Annual_Review__c revs = new INS_Annual_Review__c(ClientID__c = '003R000000Y7Gw4',
                Any_Other_info__c = 'djshfgjdh' + i,Communication__c = 'abc' + i,Sup_Net__c = 'sgdsfg' + i,Functional__c = 'Yes' + i,
                Falls__c = True,Past_History__c = 'sdfgksjf' + i,PC_ra__c = True,OT__c = True,
                Med_Dose__c = 'vcxv' + i,invOther__c = False,Med_Frequency__c = 'ageragearg' + i,
                GTS__c = False, Med_Name__c = 'dgfgdfg' + i);
                Reviews.add(revs);
                }
            System.debug('Inserting contact detail(single record validation)');
                test.StartTest();
                    insert Reviews;
                test.StopTest();
            System.assertEquals(Reviews.size()>5, true);
          
        }
       
       // checks contacts exist ans displays the right one on screen.
    Public static TestMethod void getSetCon()
        {
            pageRef = new PageReference('Annual_Review2e'); // used to set a page         
            test.setCurrentPage(pageRef);  // set starting page
            ApexPages.CurrentPage().getParameters().put('cid', '003R000000Y7Gw4');
     
         // this builds the constructor for the controller which links this to the correct object
            INS_Annual_Review__c testrev = new INS_Annual_Review__c(ClientID__c = '003R000000Y7Gw4',
                Communication__c = 'abc',Functional__c = 'Yes',PC_ra__c = True,OT__c = True,
                Med_Dose__c = 'vcxv',invOther__c = False,Med_Frequency__c = 'ageragearg',
                GTS__c = False, Med_Name__c = 'dgfgdfg');
                Insert testrev;

            ApexPages.StandardController stdController = new ApexPages.StandardController(testrev);
            AnnRevCon2edit AR = new AnnRevCon2edit(stdcontroller);
     
        // Create some test data 
        List<Contact> contacts = new List<Contact>{};
        for (Integer i = 0; i < 10; i++) {
            Contact c = new Contact(Is_Client__c = True, LastName = 'notlob' +i, Client_Address_1__c = 'Address'+i,
            Client_County__c = 'somecounty' +i, ContactType__c = 'Client' + i, Client_Town__c = 'Bolton' + i, Client_Postcode__c = 'postcode' + i,
            Client_DOB__c = System.today());
            contacts.add(c);
        }
    
        // Insert the test data - it will not be committed to the database,
        // since this is a test method
        insert contacts;
    
        System.assertEquals(contacts.size()>5, true);
     
        List<contact> the_contacts = AR.getContacts();
            system.assert(the_contacts.size()>0);
        }


        Public Static TestMethod void saveandnotes()
        {
            pageRef = new PageReference('Annual_Review2e'); // used to set a page         
            test.setCurrentPage(pageRef);  // set starting page
            ApexPages.CurrentPage().getParameters().put('cid', '003R000000Y7Gw4');
            ApexPages.CurrentPage().getParameters().put('form', 'INS_Annual_Review__c');
                       
            // this builds the constructor for the controller which links this to the correct object
            INS_Annual_Review__c testrev = new INS_Annual_Review__c(ClientID__c = '003R000000Y7Gw4',
                Communication__c = 'abc',Functional__c = 'Yes',PC_ra__c = True,OT__c = True,
                Med_Dose__c = 'vcxv',invOther__c = False,Med_Frequency__c = 'ageragearg',
                GTS__c = False, Med_Name__c = 'dgfgdfg');
                Insert testrev;

            ApexPages.StandardController stdController = new ApexPages.StandardController(testrev);
            AnnRevCon2edit AR = new AnnRevCon2edit(stdcontroller);
          
           // String nextPage = controller.saveandnotes().getUrl();

            //system.assertEquals('/apex/PN?cid=003R000000Y7Gw4', nextPage);

        }
}

The lines in red just throw an error when left in.

Thanks Steve

TechSteveTechSteve

Hi Ispita Navatar, I put your code into my test code but when I  ran it I still have the same code coverage.

 

Thanks Steve