+ Start a Discussion
tobibeertobibeer 

Testing controller extension code when 404 page

I am using the same Visualforce page and its corresponding controller extension as both the index page and the 404 page.

 

My question is: How can I test the controller extension code when used as a 404 page?

 

The following fails to create a working 404 instance:

 

Test.setCurrentPage(Page.NameOfMyIndexPageThatIsAlsoUsedAsThe404Page);
//this does not work as I cannot construct a Standardcontroller for a NULL reference
ApexPages.StandardController stdController = new ApexPages.StandardController(NULL); WebPageController controller = new WebPageController (stdController);
//should not be needed, I guess, as there is no corresponding object anyways ApexPages.currentPage().getParameters().put('id',NULL);

 

In case you're interested, to test whether or not the page is being called as a 404 page I use the following expression:

 

Boolean is404 = stdController.getId() == NULL && Site.getOriginalUrl() != NULL;

 

Cheers, Tobias.