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).

  • Out of curiosity: why would you not want to bulletproof your code to not allow that exception to occure in the first place ? Which should be possible, no ? Oct 10, 2012 at 7:23

3 Answers 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. http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_readonly_context.htm

  • 1
    I just tested and even a transient collection of over 1,000 bound to a visualforce page will throw the same CollectionLimiter exception. Are you seeing something different where this limit doesn't apply to transient collections? Oct 11, 2012 at 0:55
  • 2
    Guess I misspoke about "persisted view state" - wasn't actually sure but it doesn't surprise me that the limiter applies to Transients (kind of annoying though). I have removed the "as part of the persisted view state" from my answer for future generations.
    – jkraybill
    Oct 11, 2012 at 6:57
  • 2
    Awesome. One last comment to note that the readonly option on a visualforce page does also relax this from 1,000 to 10,000. (salesforce.com/us/developer/docs/pages/Content/…) Oct 11, 2012 at 16:35
  • Great answer +1 , @ca_peterson +1 for info about readonly option - much appreciated.
    – Jim
    Oct 11, 2012 at 19:55

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.


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"

You must log in to answer this question.

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