You need to sign in to do that
Don't have an account?
Dan 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:
I then call a test method where I don't set a required field:
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,
Dan
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:
Code:
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) { ApexPages.addMessages(de); return null; } catch (Exception e) { ApexPages.addMessages(e); return null; } return Page.ProjectCreateSuccess; }
Code:
static testMethod void testProjectCreateNoDescription() { Test.setCurrentPage(Page.ProjectWizardPage); 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,
Dan
Can somebody from Salesforce confirm the issue and provide a timeframe,when this bug will be fixed ?