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
SadminSadmin 

How to pass parameters to system.currentPagereference.getParameters().get() in Test Methods

Hi
 
I have written the below code in the controller.

public void init()
{
Bid=System.currentPageReference().getParameters().get('BilltoId');
Rid=System.currentPageReference().getParameters().get('ResellerId');
Coid=System.currentPageReference().getParameters().get('CopyId');
}

While writing test methods i am not able to pass the parameters.Its showing code does'nt have test coverage

Any help will be appreciated.

Thanks in advance.
Best Answer chosen by Admin (Salesforce Developers) 
ClaiborneClaiborne

In testing, you want to build all new records (which are automatically deleted after the test is executed)

So, following your example, create the billTo account record, which creates its id.

Account billTo = new Account(
    name = 'BillTo');
insert billTo;

Then you create the controller and visual page in test

            //**************************
// Test MyController controller
//**************************
PageReference pageRef = Page.MyPage;
Test.setCurrentPage(pageRef);

// Add parameters to page URL
ApexPages.currentPage().getParameters().put('BillToId', billTo.id);
MyController myController = new MyController();

There is an example of this in the VisualForce documentation.