+ Start a Discussion
Dan BelwoodDan Belwood 

Unit testing VF controller with ApexPages.addMessages

Hello all,

Unless I've missed a tick, I can't see how to unit test the presence of Error messages on a VF page.

Here's my controller method:

public PageReference save() {
  Code_Project__c project = new Code_Project__c(Name=this.metaData.Name__c, Description__c = this.metaData.Description__c);
  try {
   insert project;
   Project_Link__c[] links = new List<Project_Link__c>();
   links.add(new Project_Link__c(Name='Contact', URL__c=this.metaData.Contact_URL__c, Project__c=project.Id));
   links.add(new Project_Link__c(Name='Download', URL__c=this.metaData.Download_URL__c, Project__c=project.Id));
   if(this.metaData.Homepage_URL__c != '') {links.add(new Project_Link__c(Name='Homepage', URL__c=this.metaData.Homepage_URL__c, Project__c=project.Id));}
   insert links; 
  catch (System.DMLException de) {
   return null;
  catch (Exception e) {
   return null;
  return Page.ProjectCreateSuccess;

I then call a test method where I don't set a required field:
static testMethod void testProjectCreateNoDescription() {
  CreateProjectWizardController testController = new CreateProjectWizardController();
  String name = 'Test Project ' + String.valueOf(System.currentTimeMillis());
  testController.metaData.Name__c = name;
  testController.metaData.Contact_URL__c = 'mailto:test@test.com';
  testController.metaData.Download_URL__c = 'http://test_project.google.com/files/release1.zip';
  testController.metaData.Homepage_URL__c = 'http://code.google.com/p/test_project';
  String nextPage = testController.save().getUrl();
  System.assertEquals('/apex/projectcreatesuccess', nextPage);
  Code_Project__c testProject = [select Name, Description__c, Status__c, (Select Name, URL__c from Links__r) from Code_Project__c where Name = :name];
  System.assertEquals(name, testProject.Name);
  System.assertEquals('Test Description', testProject.Description__c);
  System.assertEquals('Submitted', testProject.Status__c);
  System.assertEquals(3, testProject.Links__r.size());
  for (Project_Link__c link : testProject.Links__r) {
   if (link.Name == 'Contact') {System.assertEquals('mailto:test@test.com', link.URL__c);}
   if (link.Name == 'Download') {System.assertEquals('http://test_project.google.com/files/release1.zip', link.URL__c);}
   if (link.Name == 'Homepage') {System.assertEquals('http://code.google.com/p/test_project', link.URL__c);}

When I run this test, I receive the following run-time error:
System.Exception: ApexPages.addMessages can only be called from a Visualforce page

Any ideas?

Best regards,

I'm having the same issue. Were you able to find a solution?
I am also facing the same issue.Unable to move code of production because of this issue.

Can somebody from Salesforce confirm the issue and provide a timeframe,when this bug will be fixed ?
Just in case anyone stumbles across this thread while researching VF testing (as I did) - this works as of Summer '09 (don't know if it worked earlier - just started with VF).