1

I'm trying to create a page in my test class like this:

PageReference pageRef = Page.testPage;
Test.setCurrentPage(pageRef);

But I'm getting an error:

Compilation error: Page testPage does not exist.

What I'm doing wrong here?

  • May be the page really doesn't exist. – Santanu Halder May 13 '15 at 10:11
  • Yeah, I think you're right. – anwarabq May 13 '15 at 10:24
  • 1
    A PageReference, as the name suggests is a reference to an existing page. It doesn't create pages. – Rob May 13 '15 at 10:24
  • I thought in order to test a controller I need to create a page in apex code. – anwarabq May 13 '15 at 10:26
4

"Page.testPage"

The highlighted text should always be name of valid VF page. We use this as a reference while covering the corresponding controller class. VF page cannot be created in context of Test Class.

2

To test a controller, for the most part, you never need to call Test.setCurrentPage at all. In fact I have never needed to do so, because even when I am parsing parameters from ApexPages.currentPage, I do not actually care what its URL is. It's a fairly esoteric function.

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.