Test Class Apex Page Parameters

  • 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?

    Are you setting your VF page as the current Page for the test like this....`Test.setCurrentPage(your page);`

    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 :

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

    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

    @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.

    @NZDev Yes! That is an excellent explanation.

  • 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 :(

    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.

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM

Tags used