How can an Apex test make a Visualforce request?

618 Views Asked by At

I have been reading "Understanding Testing in Apex". In the section entitled Understanding Test Data, there is a sentence that reads

If a test makes a Visualforce request, the executing test stays in the test context but runs in a different thread, so test data isolation is no longer enforced.

This is interesting and I would like to write a test class that illustrates this concept, but I am perplexed by the very first clause of the sentence: "If a test makes a Visualforce request...". How does one do this?

1

There are 1 best solutions below

0
On
PageReference pageRef = new PageReference('/apex/testVFPage');

This is how request a vf request. Response of the resulting page will be store in 'pageRef'. PageReference is used for this. The best practise to have a vf page rquest is as below.

PageReference pageRef = Page.testVFPage;

It allows the Force.com platform to detect the page dependancy.

If you need to do vf request for code coverage of your controller. Then do like this.

Test.setCurrentPageReference(Page.testPage);  //vf page request
ApexPages.currentPage().getParameters().put('id',ord.Id);  //pass parameters if needed