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
EMHDevEMHDev 

How to call setParameters from test method?

I need to set a query parameter in a test method.  The documentation makes only a quick reference to this, saying:

To set a query string parameter:

  • If you're writing a custom controller, use the setParameters() method with ApexPages.currentPage() to add a query parameter in a test method. For example:
    String key = 'name';
    String value = 'Caroline';
    ApexPages.currentPage().setParameters().put(key, value);
    Note
     The setParameters() method is only valid inside test methods.

However, when I put this in my test method, I get the error:

 

Save error: Method does not exist or incorrect signature: [System.PageReference].setParameters()    Testm62ConsoleController.cls    /m62 Sandbox/src/classes    line 80    Force.com save problem

 

Here is my test method:

 

 static testMethod void testConsoleController() {

		// Find a telesales user for the tests
        Profile p = [select id from profile where name='Telesales']; 
        User u = new User(alias = 'standt', email='standarduser@testorg.com', 
            emailencodingkey='UTF-8', lastname='Testing', profileid = p.Id, 
            languagelocalekey='en_US', 
            localesidkey='en_GB', 
            timezonesidkey='Europe/London',
            username='standarduser@testorg.com');


        System.runAs(u) {
         		
		//Set up the data
		List<Account> accs = new List<Account>();
		for (Integer i=0; i<50; i++) {
			Account a = new Account(
				Name = 'Test Account 1',
				BillingCountry = 'United Kingdom',
				Phone = '+44 (0)12679 121 5555',
				To_be_cleansed__c = true
			);
			accs.add(a);
		}
		insert accs;
		
		List<Contact> conts = new List<Contact>();
		List<Task> tasks = new List<Task>();
		// Create two contacts for each account
		for (Integer i=0; i<50; i++) {
			Contact c = new Contact();
			c.FirstName = 'Test';
			c.LastName = 'Contact 1';
			c.AccountId = accs[i].Id;
			c.MailingCountry = 'United Kingdom';
			c.Email = 'no@email11111.com';
			c.Phone = '+44 (0)1423 564888';
			conts.add(c);
		
			Contact c1 = new Contact();
			c1.FirstName = 'Test';
			c1.LastName = 'Contact 1';
			c1.AccountId = accs[i].Id;
			c1.MailingCountry = 'United Kingdom';
			c1.Email = 'no2@email11111.com';
			c1.Phone = '+32 (0) 2 679 12 11';
			conts.add(c1);
			
			// Add a task for each account
			Task t = new Task(WhatId=accs[i].Id, Type='Telephone call', Subject='Test subject', Status='Not started', 
            				Priority='Normal'); 
            tasks.add(t);
		}
	
		insert conts;
		insert tasks;
		
        PageReference pageRef = Page.newConsole;  // Instantiate telesales console page
        Test.setCurrentPage(pageRef);
      
        //Instantiate and construct the controller class.   
        m62ConsoleController controller = new m62ConsoleController();		
		Test.startTest();
		
		controller.selectedAcc = accs[4];
		controller.last();
		if (controller.getHasPreviousAcc()) controller.previousAcc();
		if (controller.getHasNextAcc()) controller.nextAcc();
		
		controller.getMyAccounts();
		controller.first();
		String ids = accs[6].Id;
		ApexPages.currentPage().setParameters().put('AccSel', ids);

		ids = conts[12].Id;
		ApexPages.currentPage().setParameters().put('ConSel', ids);

		controller.resetConts();
		controller.viewCont();
		controller.resetTsks();
		controller.viewTsk();
		
	}
    }

 Can anyone advise what I'm doing wrong? I've searched all over for more information but can't find any.

Thanks.

 

Best Answer chosen by Admin (Salesforce Developers) 
sforce2009sforce2009

it is,

 

ApexPages.currentPage().getParameters().put('key', 'value'); //it is getParameters not setParameters

All Answers

sforce2009sforce2009

it is,

 

ApexPages.currentPage().getParameters().put('key', 'value'); //it is getParameters not setParameters

This was selected as the best answer
EMHDevEMHDev

Thank you - it is obviously an error in the documentation (Salesforce please note) that caused me wasted hours.  My test method is working perfectly now and I've got the coverage needed.

 

Thanks again.

gjtorikiangjtorikian

Hi m62Dev,

 

Thanks for bringing the code error to our attention--it'll be fixed shortly.

 

In the future, please indicate which books you're receiving code samples from. Additionally, there's no setParameters() method defined in either the Apex or Visualforce guides, so the problem is only contained in that single Cookbook recipe.

EMHDevEMHDev

I tend to search Developer force rather than read the manuals when I'm looking for an answer to a specific problem.   I searched using the keywords "setting query string test method" and it was the first item in the results.  I read a number of Wiki pages on writing unit tests and they didn't address this particular issue.  So when I implemented the one reference I could find, I was puzzled when it didn't work and when I couldn't find any documentation on it - should have indicated to me that the cookbook was wrong, but I usually assume it is my mistake and not salesforce's!

ahab1372ahab1372

I recommend searching the Visualforce Developer's Guide and Apex Devloper's Guide directly, that usually lets me find what I am looking for