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
Priyanka PallepatiPriyanka Pallepati 

Apex Test Class Page Parameters

Hello all -

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;

After this I'm doing a System.assert('true,Opp.Id==null). The test class passes with this assert? I'm not sure why? The opportunity should have a ID I believe even within the test class.

Following to this code, I'm setting the id as a page parameter as below:

PageReference myVfPage = Page.AssociateECP;
    Test.setCurrentPage(myVfPage);
    ApexPages.StandardController sc = new ApexPages.StandardController(Opp);
    AssociateECPController ac = new AssociateECPController(sc);

   
    // 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); --> Again I want to set the id as the Opp.Id so that I can cover the code in my original class

Please let me know what steps I'm missing. Thank you
Best Answer chosen by Priyanka Pallepati
logontokartiklogontokartik
ok. So my bad, i overlooked few things. there is a difference between setCurrentPage ane setCurrentPageReference. 

PageReference myVfPage = Page.AssociateECP;
    Test.setCurrentPageReference(myVfPage); // use setCurrentPageReference, 
   
    ApexPages.currentPage().getParameters().put('id',Opp.Id);
    String id = ApexPages.currentPage().getParameters().get('id');
    system.assertEquals(true,id!=null);
   
    ApexPages.StandardController sc = new ApexPages.StandardController(Opp);
    AssociateECPController ac = new AssociateECPController(sc);


All Answers

logontokartiklogontokartik
Can you post your complete code? I am assuming your opportunity is not getting inserted at all. 
AshlekhAshlekh
Hi,

Use below code for test class 

Test.setCurrentPageReference(new PageReference('Page.myPage'));
System.currentPageReference().getParameters().put('id', idVariable);

http://salesforce.stackexchange.com/questions/23670/how-to-set-the-controller-id-for-the-test-code
Priyanka PallepatiPriyanka Pallepati
Hi  Karthik -  Here is my complete code

// Create test account
    Account a = new Account(Name='Test Account',BillingCity='Warrenville',BillingCountry='USA',Business_Line__c='Capital Markets');
    insert a;
    a = [SELECT Id,Name,GUP_Number__c,Business_Line__c FROM Account a WHERE Id =: a.Id]; 
    system.assert(true,a.Id);

// Create GUP Parent Account
Account a1 = new Account(Name='Test Account1',BillingCity='Warrenville',BillingCountry='USA',Business_Line__c='Capital Markets');
a1 = [select id from Account where id = :a1.Id];
system.assert(true,a1.Id);

//Create test Opportunity
Opportunity Opp = new Opportunity();
    Opp.Name = 'Test Opportiunity ';
    Opp.AccountId = a.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;
Opp = [select id from Opportunity where id = :Opp.Id];
system.assert(true,Opp.Id==null);

// update GUP Parent account as a1 on a
a.GUP_GUID__c = a1.Id;
update a;

   PageReference myVfPage = Page.AssociateECP;
    Test.setCurrentPage(myVfPage);
    ApexPages.StandardController sc = new ApexPages.StandardController(Opp);
    AssociateECPController ac = new AssociateECPController(sc);

   
    // 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);
logontokartiklogontokartik
Ok you are using system.assert(true,...) and passing true always, which is not right way, if you want to test if the Ids are inserted, try using 

system.assert(Opp.Id != null,Opp.Id); // when using assert, first parameter is your conditon and 2nd is any message. 

or use system.assertEquals(true,opp.Id != null)
Priyanka PallepatiPriyanka Pallepati
Hi Karthik - 

If I used system.assertEquals(true,opp.Id != null).. the test class doesnt fail.. that means opportunity is inserted..
but why I am not unable to set the ID right to the apex page parameter?
logontokartiklogontokartik
OK the assert to check the page parameter is not right. And in your test class you are setting parameter after you are calling original class constructor. If your code is in constructor your parameter is not set yet. Move your set parameter line to above AssociateECPController ac = new AssociateECPController(sc);
Priyanka PallepatiPriyanka Pallepati
Hi Karthik - This is the sequence I'm doing things

PageReference myVfPage = Page.AssociateECP;
    Test.setCurrentPage(myVfPage);
   
    ApexPages.currentPage().getParameters().put('id',Opp.Id);
    String id = ApexPages.currentPage().getParameters().get('id');
    system.assertEquals(true,id!=null);
   
    ApexPages.StandardController sc = new ApexPages.StandardController(Opp);
    AssociateECPController ac = new AssociateECPController(sc);

Its now not even hitting AssociateECPController... showing 0% code coverage...
logontokartiklogontokartik
ok. So my bad, i overlooked few things. there is a difference between setCurrentPage ane setCurrentPageReference. 

PageReference myVfPage = Page.AssociateECP;
    Test.setCurrentPageReference(myVfPage); // use setCurrentPageReference, 
   
    ApexPages.currentPage().getParameters().put('id',Opp.Id);
    String id = ApexPages.currentPage().getParameters().get('id');
    system.assertEquals(true,id!=null);
   
    ApexPages.StandardController sc = new ApexPages.StandardController(Opp);
    AssociateECPController ac = new AssociateECPController(sc);


This was selected as the best answer
Priyanka PallepatiPriyanka Pallepati
I did the changes now and my code looks as below

// Create test account
    Account a = new Account(Name='Test Account',BillingCity='Warrenville',BillingCountry='USA',Business_Line__c='Capital Markets');
    insert a;
    a = [SELECT Id,Name,GUP_Number__c,Business_Line__c FROM Account a WHERE Id =: a.Id]; 
    system.assert(true,a.Id);

// Create GUP Parent Account
Account a1 = new Account(Name='Test Account1',BillingCity='Warrenville',BillingCountry='USA',Business_Line__c='Capital Markets');
a1 = [select id from Account where id = :a1.Id];
system.assert(true,a1.Id);

//Create test Opportunity
Opportunity Opp = new Opportunity();
    Opp.Name = 'Test Opportiunity ';
    Opp.AccountId = a.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;
Opp = [select id from Opportunity where id = :Opp.Id];
system.assertEquals(true,opp.Id != null);

PageReference myVfPage = Page.AssociateECP;
    Test.setCurrentPageReference(myVfPage); // use setCurrentPageReference,
  
    ApexPages.currentPage().getParameters().put('id',Opp.Id);
    String id = ApexPages.currentPage().getParameters().get('id');
    system.assertEquals(true,id!=null);
  
    ApexPages.StandardController sc = new ApexPages.StandardController(Opp);
    AssociateECPController ac = new AssociateECPController(sc);

But its not even hitting AssociateECPController.. When I test run from Developer Console.. it shows 0% code coverage. 
logontokartiklogontokartik
are you getting any error? can you post your complete test class and original class' constructor to verify where its going wrong. Also please change the name of id to something else like paramId. 

    String paramId = ApexPages.currentPage().getParameters().get('id');
    system.assertEquals(true,paramId!=null);
Priyanka PallepatiPriyanka Pallepati
Here is my complete test class:

private class AssociateECPControllerTest {
   static void testAssociateECPController() {
    // Create test account
    Account a = new Account(Name='Test Account',BillingCity='Warrenville',BillingCountry='USA',Business_Line__c='Capital Markets');
    insert a;
    a = [SELECT Id FROM Account a WHERE Id =: a.Id]; 
    system.assert(true,a.Id);

// Create GUP Parent Account
Account a1 = new Account(Name='Test Account1',BillingCity='Warrenville',BillingCountry='USA',Business_Line__c='Capital Markets');
a1 = [select id from Account where id = :a1.Id];
system.assert(true,a1.Id);

//Create test Opportunity
Opportunity Opp = new Opportunity();
    Opp.Name = 'Test Opportiunity ';
    Opp.AccountId = a.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;
Opp = [select id from Opportunity where id = :Opp.Id];
system.assertEquals(true,Opp.Id != null);

// update GUP Parent account as a1 on a
a.GUP_GUID__c = a1.Id;
update a; 

//Create test ECP
ECP__c ecp = new ECP__c();
ecp.GUP_GUID__c = a1.Id;
ecp.Status_Internal__c = 'Active';
    insert ecp;
    system.assert(true,ecp.OwnerId==Opp.OwnerId);

PageReference myVfPage = Page.AssociateECP;
    Test.setCurrentPageReference(myVfPage); // use setCurrentPageReference,
  
    ApexPages.currentPage().getParameters().put('id',Opp.Id);
    String paramId = ApexPages.currentPage().getParameters().get('id');
    system.assertEquals(true,paramId!=null);
  
    ApexPages.StandardController sc = new ApexPages.StandardController(Opp);
    AssociateECPController ac = new AssociateECPController(sc);
}
}

This is my controller:

public class AssociateECPController {
public list<ECPWrapper> associateECPWrapperList {get; set;}
Opportunity oppRecord;
ECP__c selectedECP;
list<ECP__c> ecpList = new list<ECP__c>();
list<Opportunity> oppList = new list<Opportunity>(); 
   /********************************************************************************************************
    Constructor
    *********************************************************************************************************/
    public AssociateECPController (ApexPages.StandardController controller) {
     associateECPWrapperList = new list<ECPWrapper>();
        initialise();
    }
    /********************************************************************************************************
    Method to get ECP List from AccountMasterHelper
    *********************************************************************************************************/
    private void initialise() {
  string currentOppty = ApexPages.currentPage().getParameters().get('id');
  if(currentOppty != Null) {
      oppList.addAll([SELECT OwnerId,GUP_GUID__c,WCSS_ECP__c,WCSS_Corporate__c,WCSS_Bill_To__c,GUID__c FROM Opportunity where Id = :currentOppty]);
      if(oppList.size() > 0) {
       oppRecord = oppList[0];
       // get the ecp information
       ecpList = ECPHelper.getECPList(oppList);
       if(ecpList.size()== 0){
         ApexPages.Message msg = new Apexpages.Message(ApexPages.Severity.Error,Label.No_Matching_ECPs);
            ApexPages.addmessage(msg);
       }
       else {
        for(ECP__c ecp : ecpList) {
         associateECPWrapperList.add(new ECPWrapper(ecp));
        }
       }
      }
  }
 }
/*****************************************************************************************************************
    Wrapper class
    ******************************************************************************************************************/
    public class ECPWrapper {
        public ECP__c ecp {get; set;}
        public Boolean selected  {get; set;}
        // constructor
        private ECPWrapper(ECP__c ecp) {
           this.ecp = ecp;
           this.selected = false;
       }
    }
}


The test class doesnt fail... it passes with 0% coverage
logontokartiklogontokartik
OK. for a test class you need to mention @isTest and for testmethods to run you need to specify testmethod keyword. Something like below. Try running after adding these keywords. you have an option to run tests in the developer console.

@isTest
private class AssociateECPControllerTest {
   static testmethod void testAssociateECPController() {





Priyanka PallepatiPriyanka Pallepati
I had the @isTest annotation but not for the static method. It works. 
Priyanka PallepatiPriyanka Pallepati
Thanks a ton Karthik!! 
logontokartiklogontokartik
Hi, Did you know i uploaded my latest pictures on my Google Drive page today?Well, I’ve uploaded it again on my Google Drive click here to view them and tell me what you think. Regards Thanks and Regards, Kartik Viswanadha @logontokartik 972.365.7780 about.me/logontokartik * - Live as if you were to die tomorrow. Learn as if you were to live forever*