Find your content:

Search form

You are here

Test Visualforce Page With Over a Thousand Select Options


I have a custom visual force page which is throwing the error -

enter image description here

I have found the line which causes the error and know how to fix, but just need to write a test class which will recreate the error that is displayed on the visual force page. The test I have so far is this:

/*Test visualforce page with list with over a thousand select options*/
static testMethod void testOverAThousandEmailTemplates() {

    List<EmailTemplate> templatesToInsert = new List<EmailTemplate>(); 
    User u = [Select id, Username FROM User WHERE Username = 'blah' limit 1];
    for(integer i = 0; i < 2000; i++) {

        EmailTemplate testTemplates = Helper.CreateEmailTemplate('custom','body','subject', u, 'EmailTemplate' + string.valueof(i));

   insert templatesToInsert;
   PageReference pageRef = Page.MyPage;
   //Sets controller up for testing
   PageController controller = initialise();

This test works - I would expect that when I set the page reference the visualforce error would be thrown. How can I test this?

Update: I have tried getMessage() but no messages are being returned. I can't check the pages content as getContent() is disabled in tests. Even checking the URL won't work as the error page has the same URL as the valid page (checked in the GUI).

Attribution to: Jim

Possible Suggestion/Solution #1

I think the error is with respect to view state and nothing to do with the controller. You should just make sure the list size is not more than 1000. Why do you need to recreate the scenario in test class? You can check the size and manually add ApexMessage to the user.

Attribution to: Vignesh Damodharan

Possible Suggestion/Solution #2

Visualforce exceptions are mostly (or completely?) out of reach for the controller to catch. That error occurs when the visualforce page is being generated, I am not sure if the Apex Test internally generates the visualforce page. But did you look at the debug logs for the Test Result? Try searching for the keyword "EXCEPTION_THROWN", if the error occurs it should be logged as "System.VisualforceException"

Attribution to: manubkk

Possible Suggestion/Solution #3

The 1k collection limit used to apply to all Apex collections, but now only applies to collections that are sent to a Visualforce page. So you will not be able to directly reproduce this exception in a test class. (Also not really sure why you'd want to.)

The exception you're seeing is also a framework error that your page controller would be unable to detect directly. You'll need to find another solution, like adding a LIMIT 1000 statement to your query and notifying the user of this limitation if applicable.

EDIT: I was unaware of the Apex readonly annotation, but that may offer an alternative solution (if the page is indeed read-only) as it relaxes the limit to 10,000 items. Thanks @ca_peterson for pointing this out.

Attribution to: jkraybill
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content