9

I created a test Opportunity -

    Opportunity Opp = new Opportunity();
    Opp.Name = 'Test Opportiunity ';
    Opp.AccountId = acct.id;
    Opp.Business_Line__c = 'Capital Markets';
    Opp.Result_Reason__c = 'Amenities';
    Opp.CloseDate = System.today();
    Opp.StageName = 'Qualified';
    Opp.LeadSource = 'Web';
    Opp.Type = 'New Customer - New Business';
    Opp.Product_Type_Interests__c = 'ActiveDisclosure';
    insert Opp;

and then created the controller and passed onto my class:

ApexPages.StandardController sc = new ApexPages.StandardController(Opp);
AssociateECPController 

ac = new AssociateECPController(sc);

PageReference myVfPage = Page.AssociateECP;
Test.setCurrentPage(myVfPage);

// Put Id into the current page Parameters
ApexPages.currentPage().getParameters().put('id',Opp.Id);
String id = ApexPages.currentPage().getParameters().get('id');
System.assert('true',id==null);

and Put Id into the current page Parameters:

ApexPages.currentPage().getParameters().put('id',Opp.Id);

However when I run the test class the id is coming as null in the actual class and I'm not able to cover the if part.

Any ideas?

2
  • Are you setting your VF page as the current Page for the test like this....Test.setCurrentPage(your page); Commented Sep 2, 2014 at 15:53
  • Yes I am doing that : PageReference myVfPage = Page.AssociateECP; Test.setCurrentPage(myVfPage); still the id is coming as null... i did an assert like system.assert('true',opp.Id==null); after I insert the test Opportunity(insert opp;) it doesn't fail.. so when u create a test opportunity within the test class, it still should have ID right :
    – user10930
    Commented Sep 2, 2014 at 16:05

2 Answers 2

28

I realize this is a pretty old question. But in order for your parameter to work correctly, you have to instantiate the controller AFTER you set your page parameters.

PageReference myVfPage = Page.AssociateECP;
Test.setCurrentPage(myVfPage);

// Put Id into the current page Parameters
ApexPages.currentPage().getParameters().put('id',Opp.Id);

ApexPages.StandardController sc = new ApexPages.StandardController(Opp);
AssociateECPController ac = new AssociateECPController(sc);

String id = ApexPages.currentPage().getParameters().get('id');
System.assert('true',id==null);
3
  • That is not entirely correct. You just need to set the parameters prior to using them. Of course it all depends on at what point the controller accesses them
    – Eric
    Commented Sep 14, 2016 at 1:53
  • 1
    @JRiffe, Actually it is correct. Most of the time Page Parameters are accessed during the Controllers instantiation. And if the controller is instantiated before the page and parameters are set the controller will not pick said parameters and will function as desired. The test method in OP testing the id parameters value serves very little purpose and the actual controller should be what is tested, and hence the order of execution is important. Commented Dec 27, 2016 at 0:44
  • @NZDev Yes! That is an excellent explanation.
    – JRiffe
    Commented Jan 4, 2017 at 0:05
5

Try using Test.setCurrentPage(PageReference).

2
  • Yes I am doing that : PageReference myVfPage = Page.AssociateECP; Test.setCurrentPage(myVfPage); still the id is coming as null... i did an assert like system.assert('true',opp.Id==null); after I insert the test Opportunity(insert opp;) it doesn't fail.. so when u create a test opportunity within the test class, it still should have ID right :(
    – user10930
    Commented Sep 2, 2014 at 15:56
  • It definitely should have an Id. Perhaps the insert is failing silently? Try querying for all opportunities in the system to see if one exists in the test context.
    – Sean
    Commented Sep 2, 2014 at 17:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .