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

Custom Controller Test Class



I am facing an issue in writing an test method for the custom controller. 


I am not able to capture the page parameters, in the test class.Please find below the code.



public class multirecords{
    public List<Registration__c> regs {get; set;}
    public Semester_Schedule__c academic {get; set;}
    Id id = ApexPages.currentPage().getParameters().get('id');
    public multirecords(){ 
        academic=[Select Id, Name from Semester_Schedule__c where Id=:Id];
        regs = new List<Registration__c>();
        regs.add(new Registration__c(Semester_Schedule__c=academic.Id));
    public void addrow(){
        regs.add(new Registration__c(Semester_Schedule__c=academic.Id));
    public PageReference save(){
        insert regs;
        PageReference home = new PageReference('/home/home.jsp');
        return home;



any kind of help, guidance, is truly appreciated.


Thanks All



In your test method you need to set the page parameters like this:



insert testSemester;

Test.setCurrentPageReference(new PageReference('Page.myPageName')); 


Also, pretty sure you can't give a variable the name of "id":



Id semesterId = ApexPages.currentPage().getParameters().get('id');

public multirecords(){ 
        academic=[Select Id, Name from Semester_Schedule__c where Id=:semesterId ];