function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Clayton HilteClayton Hilte 

Test Validation Rules in Class successfully, don't fail the Test

Hi all -

I have several scenarios in my Test Class, one of which is EXPECTED to fail because of a Validation Rule. I want to test that the validation rule fires and prevents the update, but this also fails the entire Test Class. Is there a way that I can identify in the class that I am expecting there to be a failure for 1 particular TestMethod, but not the rest?

If the validation rule fires - this is good; don't fail the test class.
Best Answer chosen by Clayton Hilte
ShashankShashank (Salesforce Developers) 
Hi Clay,

You should handle the validation rule failure as an exception in your test class and check that the exception is the expected one.

Please see these examples for inspiration:
http://salesforce.stackexchange.com/questions/9888/triggering-a-dml-exception-for-test-coverage
https://developer.salesforce.com/forums/ForumsMain?id=906F00000008xGuIAI
http://stackoverflow.com/questions/17014430/unable-to-catch-dmlexception-in-unit-test

Thanks,
Shashank

All Answers

ShashankShashank (Salesforce Developers) 
Hi Clay,

You should handle the validation rule failure as an exception in your test class and check that the exception is the expected one.

Please see these examples for inspiration:
http://salesforce.stackexchange.com/questions/9888/triggering-a-dml-exception-for-test-coverage
https://developer.salesforce.com/forums/ForumsMain?id=906F00000008xGuIAI
http://stackoverflow.com/questions/17014430/unable-to-catch-dmlexception-in-unit-test

Thanks,
Shashank
This was selected as the best answer
Clayton HilteClayton Hilte
@Shashank -

Thanks for some of these links, the DMLException in a Catch method within the TestMethod is exactly what i was looking for. Now when the Exception matches the expected string I provided, it passes the test. Code snippet attached for help for others:

static testMethod void Trigger_LockinCheckAcctForStartUpPricingInsert3()
    {   
//
//Insert a Test Account with a Boolean field for validation parameters   
//
    test.startTest();
        try{
       
        Account a = new Account();
       
        a.Name = 'Test Lockin Account3';
        a.Start_Up_Pricing_Consumed__c = true;
       
    insert a;
       
//Insert child custom object with Status != Active or Approved
//but Parent Account pricing already consumed 
//     
    LockIns__c l = new LockIns__c();
       
        l.Account_for_lock_in__c = a.Id;
        l.Status__c = 'New';
        l.Start_Date__c = System.today();
            
    insert l;   

//Update the custom object record's "Status" to "Active" which will provide Validation Error
//because the Parent Account is already set to TRUE.
//       
        l.Status__c = 'Active';
       
    update l;
        }
        catch(Exception e){
            System.Assert(e.getMessage().contains('Validation Rule exception message goes here'));
        }
        test.stopTest();
    }