Wednesday, 11 July 2012

TESTING ApexPages.Message IN CONTROLLER

Class

public class SomeController { 
  public SomeController (ApexPages.StandardController controller) { 
    if (System.currentPageReference().getParameters(). 
        get(’someParam’)!= null { 
      ApexPages.addMessage(new ApexPages.Message 
          (ApexPages.Severity.CONFIRM, ‘Parameter Exists’)); 
    } else if (System.currentPageReference(). 
          getParameters().get(’update’)!= null) { 
      ApexPages.addMessage(new ApexPages.Message 
          (ApexPages.Severity.ERROR, 
         ’Parameter Does Not Exist’)); 
    } 
  } 
}


Test Class

@isTest
public class TestClass {
 static testMethod void testConstructorParameters_noParams() { 
    PageReference ref = 
        new PageReference(’/apex/yourVisualforcePage’); 
    Test.setCurrentPage(ref); 
    
    SomeController controller = new SomeController (null); 
        
    System.assert 
      (ApexPages.getMessages().size() == 1); 
    System.assert 
      (ApexPages.getMessages().get(0).getDetail() 
        == ‘Parameter Does Not Exist’); 
    System.assert 
      (ApexPages.getMessages().get(0).getSeverity() 
        == ApexPages.Severity.ERROR);    
  } 
  
  static testMethod void testConstructorParameters_paramExists() { 
    PageReference ref = 
        new PageReference(’/apex/yourVisualforcePage?someParam=xyz’);
    Test.setCurrentPage(ref); 
    
    SomeController controller = new SomeController (null); 
      
    System.assert 
      (ApexPages.getMessages().size() == 1); 
    System.assert 
      (ApexPages.getMessages().get(0).getDetail() 
        == ‘Parameter Exists’); 
    System.assert 
      (ApexPages.getMessages().get(0).getSeverity() 
        == ApexPages.Severity.CONFIRM);  
  }
}

No comments:

Post a Comment