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
Jenifer Alonzo 23Jenifer Alonzo 23 

Test class for page reference - only getting 28% Code Coverage

I'm working on a VF form that will allow users to quickly enter information, save, and return to a blank form. I have everything working but I can't figure out how to get test coverage on the controller extension. Many thanks for the help. 

Here's the class:
public  class saveAndNew{

     ApexPages.StandardController sController;
    public saveAndNew(ApexPages.StandardController controller) {
        sController = controller;
    }

    public PageReference save() {
        sController.save(); 
        PageReference pg = new PageReference('/apex/Workforce_Contact_Quick_Entry');
        pg.setRedirect(true);
        return pg;
    }
}

Here's the test:
@isTest
    private class saveAndNewTest {
        static testMethod void testQuickContact() 
        
        {
                        Contact myContact = new Contact ();
                	myContact.lastname 	='Test';   
                	myContact.ownerId 	= UserInfo.getUserId();
                   
                    
                
                    insert myContact;
        
       Test.StartTest(); 
  ApexPages.StandardController sc = new ApexPages.StandardController(myContact);
  saveAndNew testQuickContact = new saveAndNew(sc);

            
         PageReference pg = Page.Workforce_Contact_Quick_Entry;
         Pg.getparameters.put();
         test.setCurrentPage(pg);

               
        Test.setCurrentPage(pg);
        
           sc.save();
            
           
        }   
        
    }

 
Best Answer chosen by Jenifer Alonzo 23
v varaprasadv varaprasad
Hi Jenifer,

please call save method and check once.
saveAndNew testQuickContact = new saveAndNew(sc);
testQuickContact.save();

Hope this helps you!

Thanks
Varaprasad
For Support: varaprasad4sfdc@gmail.com

All Answers

v varaprasadv varaprasad
Hi Jenifer,

please call save method and check once.
saveAndNew testQuickContact = new saveAndNew(sc);
testQuickContact.save();

Hope this helps you!

Thanks
Varaprasad
For Support: varaprasad4sfdc@gmail.com
This was selected as the best answer
Jenifer Alonzo 23Jenifer Alonzo 23
Hi Varaprasad,

Thank you so much for your help. It looks like the save part passes. It's the PageReference part that isn't passing. Is it possible to help with this bit of test code? I appreciate any help you can offer.
public PageReference save() {
        sController.save(); 
        PageReference pg = new PageReference('/apex/Workforce_Contact_Quick_Entry');
        pg.setRedirect(true);
        return pg;

 
Jenifer Alonzo 23Jenifer Alonzo 23
Aha! I found the problem:

Here's the pageReference test that worked.  I used Varaprasad's code to voer the save as it was more elegant than mine. 
   
PageReference testPage = Page.Workforce_Contact_Quick_Entry;  
   Test.setCurrentPage(testPage);