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?

4
  • May be the page really doesn't exist. May 13, 2015 at 10:11
  • Yeah, I think you're right.
    – anwarabq
    May 13, 2015 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, 2015 at 10:24
  • I thought in order to test a controller I need to create a page in apex code.
    – anwarabq
    May 13, 2015 at 10:26

2 Answers 2

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .