3

I have a validation rule that checks if the user tries to enter a Quantity > 100. If so, the validation returns an error. I'm trying to to catch the validation exception, but it seems like the validation error is never thrown in the test class.

I updated my QTY to a value greater than 100 and I still do not see any validation error being thrown.

Here is my apex class:

 public PageReference save() 
 {  
   try {
      ..........
      .........
      update ali;
   }
   catch (Exception e) {
      // Roll the database back to before we saved ANYTHING
      Database.rollback(sp);
      // Add a page error / send an email / debug, something
      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, e.getMessage())); 
  }   
}

My apex test class:

@isTest static void ExceptionTest() 
{
   try { 
       Employee_CtrlExt controller;
       ApexPages.StandardController sc = new ApexPages.standardController(new Employee);
       controller = new Employee_CtrlExt(sc);

       List<Asset_Line_Item__c> ali = [Select Id, Quantity__c FROM Asset_Line_Item__c]; 
       ali[0].Quantity__c = 110;
       controller.save();

        //System.assert(e.getMessage().contains('Inventory error'), 'message=' + e.getMessage());

        } catch (DMLException e) {
            System.assert(e.getMessage().contains('Inventory error'), 'message=' + e.getMessage());            
        }
    }
  • What is your validation rule? How do you know it did not fire? Your code catches it and your assert is commented out. Your test will not enter its catch block – Eric Sep 29 '16 at 12:51
  • The reason I comment the assert in code because I am not sure how catch in the code or should my assert be in the catch? – Nick Kahn Sep 29 '16 at 12:55
6

You are using a local copy of ALi yet calling the controller save method so you are saving the controller version of ali which most likely meets the validation.

To assert the message was caught you would use

  1. Hasmessages method to check if there is a message
  2. Assert value of message

Pseudo code

Class

public Asset_Line_Item__c ali {get;set;} //Need to instantiate it somewhere

    public PageReference save() 
     {  
       try {
          ..........
          .........
          update ali;
       }
       catch (Exception e) {
          // Roll the database back to before we saved ANYTHING
          Database.rollback(sp);
          // Add a page error / send an email / debug, something
          ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, e.getMessage())); 
      }   
    }

TEST

@isTest static void ExceptionTest() 
{
       Employee_CtrlExt controller;
       ApexPages.StandardController sc = new ApexPages.standardController(new Employee);
       controller = new Employee_CtrlExt(sc);

       controller.ali.Quantity__c = 110;
       controller.save();

        System.assertEquals(true, ApexPages.hasMessages(),'No validation message was generated');
        system.assertEquals(true,ApexPages.getMessages()[0].getDetail().containsIgnoreCase('PART OF YOUR MESSAGE HERE','No validation message was generated'));

 }
  • Eric, When you get chance can you post a sample code that you talk about. I just want to know where I'm doing wrong. – Nick Kahn Sep 29 '16 at 13:14
  • @NickKahn - See updated answer. Pseudo code only – Eric Sep 29 '16 at 13:28
  • @AdrianLarson - I did not look at that as part of the answer, however, would it not depend on your requirements? You may want to show INFO severity or similar depending on design. What does your example add other than simplicity? - Yea, I removed the try catch that OP originally had in there thanks – Eric Sep 29 '16 at 13:46
  • Just simplicity, yeah. It's a heck of a lot of boilerplate. I didn't notice it was already in the OP. – Adrian Larson Sep 29 '16 at 13:47
  • 1
    First reasonable answer I'm reading on testing page messages. – Paul T. Rawkeen Jan 19 '18 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.