1

I am a newbie to salesforce and currently is learning Apex Test Classes. Here i came up with a situation that when i use

accId = ApexPages.currentPage().getParameters().get('account');

constructor, and try putting the parameter value by using

System.currentPageReference().getParameters().put('account', Id.valueOf(myaccount.Id));

in my test class, i get the value null, but when i try placing the above line i.e

accId = ApexPages.currentPage().getParameters().get('account');

in any method other than constructor, i works fine and returns the desired ID.

Any reason behind it?

The Extension

public class ContactPageExtensionNew {

    public Id accId {get; set;}
    public Contact newContact {get; set;}

    public ContactPageExtensionNew(ApexPages.StandardController controller) {
         this.newContact = (Contact)Controller.getRecord();         
    }
    public PageReference save(){        
        if(newContact != null){
            accId = ApexPages.currentPage().getParameters().get('account');
            newContact.AccountId = accId;
            insert newContact;
            PageReference nextPage = new PageReference('/' + accId);
            return nextPage;
        }
        return null;
    }

}

Here is the test class

@isTest
public class ContactButtonOnAccountExtensionTest {

    public static Account myaccount;
    public static Contact myContact;

    @isTest
    public static void positivetestoncontact(){    
        myaccount = new Account(
            Name = 'My Account'
        );
        insert myaccount; 
        myContact = new Contact(
            Firstname = 'New Contact',
            Lastname = 'Contact Lastname'
            //,AccountId = myaccount.Id
        );
        Test.startTest();        
        ApexPages.StandardController sc = new ApexPages.StandardController(myContact);
        ContactPageExtensionNew testcontact = new ContactPageExtensionNew(sc);
        PageReference pageRef = Page.NewContactPage;        
        Test.setCurrentPageReference(pageRef);
        System.currentPageReference().getParameters().put('account', Id.valueOf(myaccount.Id));

        String nexturl = testcontact.save().getUrl(); //call all your function here
        System.debug('Next URL --> ' + nexturl);
        Test.StopTest();

        System.assert(myaccount.Id != null, 'The contact is being saved!');
        System.assertEquals(myContact.FirstName, 'New Contact');
        System.assertEquals('/'+ myaccount.Id, nexturl);

    }

}
  • is this error exist during in your normal execution also? The main thing is to consider is PUT must happen before GET. – Safiya PV Apr 24 '17 at 7:08
  • No error in normal execution. the code works fine – Saumya Gaikwad Apr 24 '17 at 7:11
  • So it means your constructor with get statement execute before your put statement in your test class – Safiya PV Apr 24 '17 at 7:45
0

The reason you are not getting value in your constructor is you have already called the constructor before you set the value using parameter. So you need to change the order to make it work.

PageReference pageRef = Page.NewContactPage;        
Test.setCurrentPageReference(pageRef);
System.currentPageReference().getParameters().put('account', Id.valueOf(myaccount.Id));
ApexPages.StandardController sc = new ApexPages.StandardController(myContact);
ContactPageExtensionNew testcontact = new ContactPageExtensionNew(sc);

Now you will get parameter value in your constructor as well.

  • 1
    Got my solution... Thankyou – Saumya Gaikwad Apr 24 '17 at 7:14
0

Make sure in your test class you set the page context:

PageReference myPage = Page.YOURVFPAGE;
myPage.getParameters().put('account', myaccount.Id);
System.Test.setCurrentPage(myPage);
  • Yes It is. But again, i can access the parameter but only from a method like save() or mymethod().... but not from constructor – Saumya Gaikwad Apr 24 '17 at 6:41
  • If you're using a standard controller then refer to @Santanu's answer – Boris Bachovski Apr 24 '17 at 6:43
  • The code i placed in save() method ie. accId = ApexPages.currentPage().getParameters().get('account');, it is working fine here, but when i try placing it in constructor, it does not work. – Saumya Gaikwad Apr 24 '17 at 6:53
0

You can prepare test data like this:

Account acct = new Account(Name='testAccount');  
insert acct;
PageReference page = Page.MyVFP;
Test.setCurrentPage(page);
ApexPages.currentPage().getParameters().put('account',acct.Id);

ApexPages.StandardController standrdcontroller = new ApexPages.StandardController(acct);
YourControllerClass myController = new YourControllerClass (standrdcontroller);
System.assertEquals(.....);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.