You need to sign in to do that
Don't have an account?
Benzy
Help with a test class please with page parameter
Hi, I'm struggling to get a test class to work. Any help would be much appreciated!
Where I'm stuck is with the apex page parameter. In the test class I am trying to define this parameter and then pass it into the class (the last section of the test class). But I am totally stuck on how to get this to work.
Apex Class
Test Class (work in progress)
Where I'm stuck is with the apex page parameter. In the test class I am trying to define this parameter and then pass it into the class (the last section of the test class). But I am totally stuck on how to get this to work.
Apex Class
public class InterviewsListDataTable { String Id = ApexPages.currentPage().getParameters().get('Id'); public List<Interview_Time_Slot__c> interviewList { get { if (interviewList == null) { interviewList = [SELECT Interviewer_Name__c, Date_and_Time_of_Slot__c, Interviewer_Email__c, Interviewer_Skype_Name__c, Program__r.Program_Public_Name__c FROM Interview_Time_Slot__c WHERE Program__c =: Id ORDER BY Date_and_Time_of_Slot__c Asc]; } return interviewList; } set; } }
Test Class (work in progress)
@isTest private class TestInterviewsListDataTable {static testMethod void myUnitTest() {Profile pf = [Select Id from Profile where Name = 'System Administrator'];User u = new User(); u.FirstName = 'Test'; u.LastName = 'User'; u.Email = 'testuser@test123456789.com'; u.CompanyName = 'test.com'; u.Title = 'Test User'; u.Username = 'testuser@test123456789.com'; u.Alias = 'testuser'; u.CommunityNickname = 'Test User'; u.TimeZoneSidKey = 'America/Mexico_City'; u.LocaleSidKey = 'en_US'; u.EmailEncodingKey = 'ISO-8859-1'; u.ProfileId = pf.Id; u.LanguageLocaleKey = 'en_US'; insert u; system.runAs(u){ //Set RecordTypeId variable CA String strpRecordTypeId = [Select Id From RecordType WHERE DeveloperName = 'Fellowship' AND sobjectType = 'Program__c'].Id; Program__c program = new Program__c(); program.Name = 'TestProgram'; program.RecordTypeId = strpRecordTypeId; Insert program; Interview_Time_Slot__c objInterviewSlot = new Interview_Time_Slot__c(); objInterviewSlot.Program__c = program.Id; Insert objInterviewSlot; PageReference PageRef = Page.Interviews; Test.setCurrentPage(PageRef); ApexPages.currentPage().getParameters().put('Id', 'program.Id'); InterviewsListDataTable testlist = new InterviewsListDataTable(); testlist.Id = program.Id; testlist.interviewList(); } } }
Updated code is below, try this one
All Answers
You can create the necessary record and then you can set the page id with below code
ApexPages.currentPage().getParameters().put('Id', obj.id);
Then it will cover the apex code..
Please check and let me know if need anything
Thanks
Sandeep
ApexPages.currentPage().getParameters().put('Id', program.Id); not write as 'program.id' in string. select this as a best answer if it solve your problem.
Please refer order of execution of vf page.
Once you instantiate a class ,properties gets executed automatically and there is no order of execution.
Your code will work not behave same all the time.
Please try below to set Id value as soon as class instance is created.
And no need to create the controller instance explicitily ,it will be invoked from page itself like you have written below in test class.
Let us know if it helps.
I have created the Program__c record (line 24)
I have also included your suggestion (line 36)
I get the following error: Error: Compile Error: Variable is not visible: Id at line 39 column 6
Please refer above code...
Thanks
Ashish, thanks I have used your code for the apex class and it works well.
For you both, I now get a different error on the test class (using my previous test class and using Arunkumar's test class):
Error: Compile Error: Method does not exist or incorrect signature: [InterviewsListDataTable].interviewList() at line 41 column 6
This is strange because it is definitely the right name of the lift in the apex class. Any ideas?
Thanks
Updated code is below, try this one
That worked perfectly, thank you!