Skip to content

UTIL_ValidationTestHelper

Class · Group: Testing

apex
global inherited sharing class UTIL_ValidationTestHelper

Reusable utility class for testing validation rules. This is NOT a test class itself but provides assertion methods for subscriber test classes and the framework's own test classes to verify validation rule behavior. The class validates records in-memory using the validation framework and provides focused assertions for specific rule failures or passes.

Since: 1.0

Example:

apex
@IsTest
private static void shouldRequireEmail()
{
    Account account = new Account(Name = 'Test');
    UTIL_ValidationTestHelper.assertRuleFails(account, 'Account_Requires_Email');
}
@IsTest
private static void shouldPassWhenEmailProvided()
{
    Account account = new Account(Name = 'Test', Email__c = 'test@test.com');
    UTIL_ValidationTestHelper.assertRulePasses(account, 'Account_Requires_Email');
}

See Also: UTIL_ValidationRule


Methods

MethodDescription
global static void assertRuleFails(SObject record, SObject oldRecord, String ruleDeveloperName, TriggerOperation operation)Validates a record and asserts that a specific rule failed, with custom operation.
global static void assertRuleFails(SObject record, String ruleDeveloperName)Validates a record and asserts that a specific rule failed.
global static void assertRulePasses(SObject record, SObject oldRecord, String ruleDeveloperName, TriggerOperation operation)Validates a record and asserts that a specific rule did NOT fail, with custom operation.
global static void assertRulePasses(SObject record, String ruleDeveloperName)Validates a record and asserts that a specific rule did NOT fail.
global static UTIL_ValidationRule.ValidationResult validate(SObject record)Validates a record and returns the full result for advanced assertions.
global static UTIL_ValidationRule.ValidationResult validate(SObject record, SObject oldRecord, TriggerOperation operation)Validates a record and returns the full result for advanced assertions with operation context.

Method Details

assertRuleFails

apex
global static void assertRuleFails(SObject record, SObject oldRecord, String ruleDeveloperName, TriggerOperation operation)

Validates a record and asserts that a specific rule failed, with custom operation.

Parameters:

  • record (SObject) - The new record to validate
  • oldRecord (SObject) - The old version of the record (for update scenarios)
  • ruleDeveloperName (String) - The DeveloperName of the validation rule expected to fail
  • operation (TriggerOperation) - The trigger operation context

Throws:

Since: 1.0

Example:

apex
Account oldAccount = new Account(Name = 'Test', Status__c = 'Draft');
Account newAccount = oldAccount.clone();
newAccount.Status__c = 'Active';
UTIL_ValidationTestHelper.assertRuleFails(newAccount, oldAccount, 'Account_Cannot_Reactivate', TriggerOperation.BEFORE_UPDATE);

assertRuleFails

apex
global static void assertRuleFails(SObject record, String ruleDeveloperName)

Validates a record and asserts that a specific rule failed. The record is validated in-memory against all applicable validation rules, then the assertion verifies that the specified rule produced an error.

Parameters:

  • record (SObject) - The record to validate
  • ruleDeveloperName (String) - The DeveloperName of the validation rule expected to fail

Throws:

Since: 1.0

Example:

apex
Account account = new Account(Name = 'Test');
UTIL_ValidationTestHelper.assertRuleFails(account, 'Account_Requires_Email');

assertRulePasses

apex
global static void assertRulePasses(SObject record, SObject oldRecord, String ruleDeveloperName, TriggerOperation operation)

Validates a record and asserts that a specific rule did NOT fail, with custom operation.

Parameters:

  • record (SObject) - The new record to validate
  • oldRecord (SObject) - The old version of the record (for update scenarios)
  • ruleDeveloperName (String) - The DeveloperName of the validation rule expected to pass
  • operation (TriggerOperation) - The trigger operation context

Throws:

Since: 1.0

Example:

apex
Account oldAccount = new Account(Name = 'Test', Status__c = 'Active');
Account newAccount = oldAccount.clone();
newAccount.Status__c = 'Inactive';
UTIL_ValidationTestHelper.assertRulePasses(newAccount, oldAccount, 'Account_Cannot_Reactivate', TriggerOperation.BEFORE_UPDATE);

assertRulePasses

apex
global static void assertRulePasses(SObject record, String ruleDeveloperName)

Validates a record and asserts that a specific rule did NOT fail. The record is validated in-memory against all applicable validation rules, then the assertion verifies that the specified rule did not produce an error.

Parameters:

  • record (SObject) - The record to validate
  • ruleDeveloperName (String) - The DeveloperName of the validation rule expected to pass

Throws:

Since: 1.0

Example:

apex
Account account = new Account(Name = 'Test', Email__c = 'test@test.com');
UTIL_ValidationTestHelper.assertRulePasses(account, 'Account_Requires_Email');

validate

apex
global static UTIL_ValidationRule.ValidationResult validate(SObject record)

Validates a record and returns the full result for advanced assertions. Use this when you need to make multiple assertions or custom assertions on a single validation run.

Parameters:

  • record (SObject) - The record to validate

Returns: UTIL_ValidationRule.ValidationResult - The ValidationResult containing all errors/warnings

Since: 1.0

Example:

apex
Account account = new Account(Name = 'Test');
UTIL_ValidationRule.ValidationResult result = UTIL_ValidationTestHelper.validate(account);
Assert.areEqual(2, result.errors.size(), 'Expected 2 validation errors');

validate

apex
global static UTIL_ValidationRule.ValidationResult validate(SObject record, SObject oldRecord, TriggerOperation operation)

Validates a record and returns the full result for advanced assertions with operation context.

Parameters:

  • record (SObject) - The new record to validate
  • oldRecord (SObject) - The old version of the record (for update scenarios)
  • operation (TriggerOperation) - The trigger operation context

Returns: UTIL_ValidationRule.ValidationResult - The ValidationResult containing all errors/warnings

Since: 1.0

Example:

apex
Account oldAccount = new Account(Id = '001000000000001', Name = 'Test');
Account newAccount = oldAccount.clone(true, true, true, true);
newAccount.Name = 'Updated';
UTIL_ValidationRule.ValidationResult result = UTIL_ValidationTestHelper.validate(newAccount, oldAccount, TriggerOperation.BEFORE_UPDATE);
Assert.isTrue(result.isValid, 'Expected record to pass validation');