You need to sign in to do that
Don't have an account?
How can a test make a Visualforce request?
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 to share with my coworkers, but I am perplexed by the very first clause of the sentence: "If a test makes a Visualforce request...". How does one do this?
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_System_PageReference_ctor_2.htm
You can make a VisualForce request using the PageReference Constructor like in the following example:
In this way, The test class will actually make a request to the to the Visualforce page, and the response (the resulting page) is stored in the PageReference object. Any controllers that are attached to that Page actually get instantiated and run - data gets created (if your controller does such) and the page gets returned. However, if there is Javascript on the page, it will not run (because this is a function of the browser, not of a Salesforce server) and therefore if your page creates Salesforce records in Javascript (like, for example, Visualforce Remoting) - this data will not exist, or at least according to the Salesforce Documentation - not be accessible in the test method.
In summary - do not depend on Calling a Visualforce page in your test to create more data for your test method. Also, the example above is not the preferable way to call a page either. Best Practice would be the following because it allows the Force.com platform to detect the page 'dependancy':
If you are trying to create code coverage for your Controllers, the best practice way would most likely be the following: I hope this helps!