You need to sign in to do that
Don't have an account?
![KevSnell KevSnell](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
Test Method Apex Form Help!
Hi All,
I have a simple form with a customer controller which I now need to create the test code so I can deploy to live system, however, I'm havig some issues creating the test method as I'm relatively new to testing. So I'm hoping someone can help as I can't seem to find the answer anywhere.
The VisualForce Page is called New_Reserve_Entry.
And here is the Reserve Controller Code:
public with sharing class Reserve { public Lead lead {get; private set;} public String company {get; set;} public String email {get; set;} public String phone {get; set;} public String firstName {get; set;} public String lastName {get; set;} public String title {get; set;} public string primary_campaign {get; set;} public String getName() { return 'Reserve'; } // save button is clicked public PageReference save() { Lead l = new Lead( OwnerId = '00GE0000000hCBH', LeadSource = 'Reserve', FirstName = firstName, LastName = lastName, Email = email, Phone = phone, Title = title, Company = company,
); try { insert l; // inserts the new record into the database } catch (Exception e) { ApexPages.addMessage(new ApexPages.message(ApexPages.severity.Error,'Error adding Candidate to Reserve List.')); return null; } return Page.New_Reserve_Entry_Confirmation; } }
@isTest private with sharing class Reserve_Test { public static testMethod void testReserve_Test() { PageReference pageRef = Page.New_Reserve_Entry; Test.setCurrentPage(pageRef); reserve controller = new reserve(); String nextPage = controller.save().getUrl(); //Controller and Data controller = new reserve(); Lead l = new Lead( OwnerId = '00GE0000000hCBH', LeadSource = 'Reserve', FirstName = 'FirstName', LastName = 'LastName', Email = 'test@test.com', Phone = '001', Title = 'Junior', Company = 'Acme' ); //Save nextPage = controller.save().getUrl(); // Verify Lead[] leads = [select id, email from lead where Company = 'Mystry']; System.assertEquals('firstlast@acme.com', leads[0].email); System.assertEquals('lastname',leads[0].LastName); } }
So I understand that the test code must call the page, load the controller, insert the lead data, save, confirm return page and check data was inserted. However, writing it is the problem:
I got this far and then got stuck: Currently I get an error for string nextpage = controller.save().getUrl() saying "Attempt to de-reference a null object"
I would really appreciate if someone could help me out either by fixing the test code, showing me a test code they have for something similar or any links that could help.
Thanks
Kev
Hi,
Please try the below code. I have commented out the ownerId beacuse it automatically sets when we create a record.
Please mark this as accepted solution if its working fine. Please let me know if there is any issue.
Thanks,
Devendra Natani
All Answers
Hi,
There are couple of mistakes in your code.
Please let me know if there is any issue.
Thanks,
Devendra Natani
Blog
Thanks for the advice, however, my coding skills are very basic and I really need some help to complete the test method. Is it possible you could show me what you mean?
Can you please share the complete of this class?
Thanks,
Devendra Natani
Here is the Apex Class:
Also here is the Visualforce Page Code for Reference called New_Reserve_Entry
Hi,
Please try the below code. I have commented out the ownerId beacuse it automatically sets when we create a record.
Please mark this as accepted solution if its working fine. Please let me know if there is any issue.
Thanks,
Devendra Natani
Thanks Devendra, now just got to finish some other tests.