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
RimaRima 

Apex test method

Hi All,

 

I am trying to write a test method for controller extension Apex class.I want to test the "save" functionality of an Apex class.Follwing is the code.

public class jobcntroller {

 

    public Job_Details__c job{ get; set; }

     public String CompanyName;

     public void setCompanyName(String CompanyName)

    {

        this.CompanyName = CompanyName;

    }

    ApexPages.StandardController controller;   

    String s;

 

 

 

 

    public jobcontroller(ApexPages.StandardController stdController) {

        job = new Job_Details__c ();    

        controller = stdController;

 

    }   

 

   public PageReference mysave() {

     Schema.DescribeSObjectResult result = ob_Details__c.sObjectType.getDescribe();    

     controller.save();

 

 

    PageReference pageRef = New PageReference('/' + result.getKeyPrefix() + '/e');

 

     pageRef.setRedirect(true);

     return pageRef;

 

     }

      static testMethod void testMyController() {

      //Use the PageReference Apex class to instantiate a page

        PageReference pageRef = Page.jobpage;

        //In this case, the Visualforce page named 'jobpage' is the starting point of this test method.

        Test.setCurrentPage(pageRef);

        //Instantiate and construct the controller class. 

       ApexPages.StandardController thecontroller;       

        jobcontroller controller = new jobcontroller(thecontroller);

        controller.setCompanyName('abc');

        //The .getURL will return the page url the Save() method returns.

        String nextPage = controller.mysave().getUrl();

        //Check that the save() method returns the proper URL.

        System.assertEquals('/apex/failure?error=noParam', nextPage);

 

     }

 

     }

 

When I do the runtest, I am getting the following message:

System.NullPointerException: Attempt to de-reference a null object

 

stack trace:

Class.jobcntroller.mysave: line 25, column 25

Class.jobcntroller.testMyController: line 44, column 27 External entry point

 

I am not able to correct the nullpointerexception. Any help on how to resolve this is greatly appreciated.

 

Best Answer chosen by Admin (Salesforce Developers) 
SargeSarge

For that you need to slightly change the approach. Try out the following code

 

 

public class jobcntroller {

 

     public Job_Details__c job{ get; set; }

     public String CompanyName;

     public void setCompanyName(String CompanyName)

     {

        this.CompanyName = CompanyName;

     }

     //ApexPages.StandardController controller;   

     String s;
 

 

    public jobcontroller(ApexPages.StandardController stdController) {

        job = (Job_Details__c)stdController.getRecord();
        

    }   

 

   public PageReference mysave() {

      Schema.DescribeSObjectResult result= job_Details__c.sObjectType.getDescribe();    
 
     // controller.save();
	PageReference pageRef = null;
     try{ 
     	insert job; //insert job instance with fields populated from VF in inputFields
       	pageRef = New PageReference('/' + result.getKeyPrefix() + '/o'); 
        //eg for account it is /001/o for account list view
        pageRef.setRedirect(true);
}catch(DmlException d){ //handle the exception } return pageRef; } static testMethod void testMyController() { //Use the PageReference Apex class to instantiate a page PageReference pageRef = Page.jobpage; //In this case, the Visualforce page named 'jobpage' is the starting point of this test method. Test.setCurrentPage(pageRef); //set the field values of job details
         //all the field values those are entered in VF page has to be set here
        Job_Details__c jTest = new Job_Details__c(name='Test Job'); //Instantiate and construct the controller class. ApexPages.StandardController thecontroller = new ApexPages.StandardController(jTest); jobcontroller controller = new jobcontroller(thecontroller); controller.setCompanyName('abc'); //The .getURL will return the page url the Save() method returns. String nextPage = controller.mysave().getUrl(); //Check that the save() method returns the proper URL. System.assertEquals('/apex/failure?error=noParam', nextPage); } }

 Please note the code changes highlighted in red.

 

Hope this approach works for you.

 

All Answers

SargeSarge

Hi,

 

   You need to first instatiate standardController in your test method, not just declare it.

 

ApexPages.StandardController controller = new ApexPages.standardController(new job_Details__c());

 

Hope this helps

 

 

RimaRima

Hi,

 

Thank you for your response.That's great! it solved NULL pointer exception. However, it is not saving the data I enter for the custom object. When I clicked on save and new button, it is not saving the data I entered. When I click on the newly created record it does not have any data in the fields. It saves the data if I remove the initialization statement, but gives me null pointer exception.Could anyone please let me know how to save the data in this situation? Your help is greatly appreciated.

SargeSarge

For that you need to slightly change the approach. Try out the following code

 

 

public class jobcntroller {

 

     public Job_Details__c job{ get; set; }

     public String CompanyName;

     public void setCompanyName(String CompanyName)

     {

        this.CompanyName = CompanyName;

     }

     //ApexPages.StandardController controller;   

     String s;
 

 

    public jobcontroller(ApexPages.StandardController stdController) {

        job = (Job_Details__c)stdController.getRecord();
        

    }   

 

   public PageReference mysave() {

      Schema.DescribeSObjectResult result= job_Details__c.sObjectType.getDescribe();    
 
     // controller.save();
	PageReference pageRef = null;
     try{ 
     	insert job; //insert job instance with fields populated from VF in inputFields
       	pageRef = New PageReference('/' + result.getKeyPrefix() + '/o'); 
        //eg for account it is /001/o for account list view
        pageRef.setRedirect(true);
}catch(DmlException d){ //handle the exception } return pageRef; } static testMethod void testMyController() { //Use the PageReference Apex class to instantiate a page PageReference pageRef = Page.jobpage; //In this case, the Visualforce page named 'jobpage' is the starting point of this test method. Test.setCurrentPage(pageRef); //set the field values of job details
         //all the field values those are entered in VF page has to be set here
        Job_Details__c jTest = new Job_Details__c(name='Test Job'); //Instantiate and construct the controller class. ApexPages.StandardController thecontroller = new ApexPages.StandardController(jTest); jobcontroller controller = new jobcontroller(thecontroller); controller.setCompanyName('abc'); //The .getURL will return the page url the Save() method returns. String nextPage = controller.mysave().getUrl(); //Check that the save() method returns the proper URL. System.assertEquals('/apex/failure?error=noParam', nextPage); } }

 Please note the code changes highlighted in red.

 

Hope this approach works for you.

 

This was selected as the best answer
RimaRima

Hi,

 

It is a perfect solution, and it is saving the records. I greatly appreciate your help.