You need to sign in to do that
Don't have an account?
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.
For that you need to slightly change the approach. Try out the following code
Please note the code changes highlighted in red.
Hope this approach works for you.
All Answers
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
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.
For that you need to slightly change the approach. Try out the following code
Please note the code changes highlighted in red.
Hope this approach works for you.
Hi,
It is a perfect solution, and it is saving the records. I greatly appreciate your help.