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
BenzyBenzy 

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
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();

    }
}
}

 
Best Answer chosen by Benzy
Arunkumar RArunkumar R
Hi Benz,

Updated code is below, try this one
 
@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();   

   // In order to cover, Apex Property you need to assign like below. In your property you have used return type as list of interview time slot object.
     List<Interview_Time_Slot__c> interviewList = testlist.interviewList;

    }
}
}

 

All Answers

sandeep sankhlasandeep sankhla
Hi 

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
Ravikant kediaRavikant kedia
You have to use below line to set current page parameter.
 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.
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Benzy,

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.
public class InterviewsListDataTable {

    public String 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;
    }
	public InterviewsListDataTable(){
		Id = ApexPages.currentPage().getParameters().get('Id');
	}
}

And no need to create the controller instance explicitily ,it will be invoked from page itself like you have written below in test class.
PageReference PageRef = Page.Interviews;

Let us know if it helps.


 
BenzyBenzy
Hi Sandeep

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
sandeep sankhlasandeep sankhla
Hi benzy,
 
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;

          
     
     ApexPages.currentPage().getParameters().put('Id', program.Id);

 

     InterviewsListDataTable testlist = new InterviewsListDataTable();

Please refer above code...

Thanks
BenzyBenzy
Hi Ashish Sharma and Arunkumar,

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
Arunkumar RArunkumar R
Hi Benz,

Updated code is below, try this one
 
@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();   

   // In order to cover, Apex Property you need to assign like below. In your property you have used return type as list of interview time slot object.
     List<Interview_Time_Slot__c> interviewList = testlist.interviewList;

    }
}
}

 
This was selected as the best answer
BenzyBenzy
Thanks Arunkumar

That worked perfectly, thank you!