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
Kasia Wojewodzka 7Kasia Wojewodzka 7 

Test Class for Validation rule Issue: System.AssertException: Assertion Failed: Validation rule should throw correct error.: Expected: true, Actual: false

Dear Community  

I am buiding the test class for Validation rule that block users with Permission Set assigned and proper record type from editing certaint  fields. 

I keep running into the issue.  
Error Message   System.AssertException: Assertion Failed: Validation rule should throw correct error.: Expected: true, Actual: false
Stack Trace              Class.Test_VR_FSRNAAssetBlockEdit.Test_FSRNABlockPIMSEdit: line 215, column 1

Where I testing update of the Account ID.  
Line 215 is failing
System.assertEquals(true, validError, 'Validation rule should throw correct error.');

Any advise on how to fix the error would be much appriciated? 
Thnak you in advance. 
Kasia 
@isTest
public class Test_VR_FSRNAAssetBlockEdit {
    
    
    private class TestException extends Exception {}

    static final string PERM_SET = 'FSR_NA_DBR_Setup_and_Installation_Permissions';
    
    
    @testSetup
    public static void dataSetup() {
      // create test user with Field Support Rep NA profile 
    Profile FSRNA = [SELECT Id, Name FROM Profile WHERE Name = 'Field Support Rep - CAG'];
        

        User u = new User(
            Alias = 'CTSvTest',
            Email = 'TestAssetValidationRuleFSRNA@XXXX.com',
            Username = 'TestAssetValidationRS@XXXX.com',
            FirstName = 'CTS Test',
            LastName = 'UserAssetVR',
            TimeZoneSidKey = 'America/New_York',
            LocaleSidKey = 'en_US',
            EmailEncodingKey = 'UTF-8',
            LanguageLocaleKey = 'en_US',
            ProfileId = FSRNA.Id
        );

        insert u;
        
        // need to assign user to permission set
        PermissionSet ps = [SELECT Id FROM PermissionSet WHERE Name = :PERM_SET];
    
        PermissionSetAssignment psa = new PermissionSetAssignment(
        PermissionSetId = ps.Id,
        AssigneeId = u.Id
        );
        insert psa;
        
        // Make Account
        Account acc = new Account();
        acc.Name = 'TestAccount';
        acc.BillingCity ='Boston' ;
        acc.BillingCountry='United States';
        acc.BillingPostalCode='600075';
        acc.BillingState=' Massachusetts';
        acc.BillingStreet='water well street';
        acc.ShippingCity ='Boston' ;
        acc.ShippingCountry='United States';
        acc.ShippingPostalCode='600075';
        acc.ShippingState=' Massachusetts';
        acc.ShippingStreet='water well street'; 
        acc.TIN__c = '1';

        System.runAs(u) {
            insert acc;
        }
    }  
    @IsTest
    static void Test_FSRNABlockPIMSEdit () {
        // Get Record Type
        Id assetRecordTypeId = Schema.SObjectType.Asset.getRecordTypeInfosByDeveloperName().get('XXXX_Practice_Information_Mgmt').getRecordTypeId();
        User u = [ SELECT Id FROM User WHERE Username = 'TestAssetValidationRS@XXXX.com' LIMIT 1 ];
        Account acc = [ SELECT Id FROM Account WHERE Name = 'TestAccount' LIMIT 1 ];

        Asset product = new Asset();
        product.Status = 'Installed';
        product.Is_Active__c = true;
        product.SerialNumber = 'TA001';
        product.RecordTypeId = assetRecordTypeId;
        product.AccountId = acc.Id;
        product.Asset_Category__c = 'Practice Software';
        product.Activation_Key__c = '1234-5678-90';
        product.Name = 'Cornerstone';
        insert product;

        // Test edit of activation key
        try {
            System.runAs(u) {
                product.Name = 'Cornerstone';
                product.Activation_Key__c = '0912-5678-90';
                update product;
        }
            throw new TestException('Failed to throw correctly');
        } catch (Exception e) {
            String errorMessage = e.getMessage();
            Boolean validError = errorMessage.contains('You cannot edit XXXX Products and Services record.');
            System.assertEquals(true, validError, 'Validation rule should throw correct error.');
        }

        System.runAs(u){
            product.Activation_Key__c = '0912-5678-90';
        }

        // Test edit of PIMS installed sw version
        try {
            System.runAs(u) {
                product.Name = 'Cornerstone';
                product.PIMS_Installed_SW_Version__c = '8.5.01.2';
                update product;
        }
            throw new TestException('Failed to throw correctly');
        } catch (Exception e) {
            String errorMessage = e.getMessage();
            Boolean validError = errorMessage.contains('You cannot edit XXXX Products and Services record.');
            System.assertEquals(true, validError, 'Validation rule should throw correct error.');
        }

        System.runAs(u){
            product.PIMS_Installed_SW_Version__c = '8.5.01.3';
        }

        // Test edit of PIMS primary OS
        try {
            System.runAs(u) {
                product.Name = 'Cornerstone';
                product.PIMS_Primary_Operating_System__c = 'Macintosh';
                update product;
        }
            throw new TestException('Failed to throw correctly');
        } catch (Exception e) {
            String errorMessage = e.getMessage();
            Boolean validError = errorMessage.contains('You cannot edit XXXX Products and Services record.');
            System.assertEquals(true, validError, 'Validation rule should throw correct error.');
        }

        System.runAs(u){
            product.PIMS_Primary_Operating_System__c = 'Windows';
        }

        // Test edit of Service Level
        try {
            System.runAs(u) {
                product.Name = 'Cornerstone';
                product.PIMS_Service_Level__c = 'Gold';
                update product;
        }
            throw new TestException('Failed to throw correctly');
        } catch (Exception e) {
            String errorMessage = e.getMessage();
            Boolean validError = errorMessage.contains('You cannot edit XXXX Products and Services record.');
            System.assertEquals(true, validError, 'Validation rule should throw correct error.');
        }

        System.runAs(u){
            product.PIMS_Service_Level__c  = 'Silver';
        }

       
       // Test edit of XXXX Unique Key
        try {
            System.runAs(u) {
                product.Name = 'Cornerstone';
                product.SAP_Cust_No_Type_ID__c = '1';
                update product;
        }
            throw new TestException('Failed to throw correctly');
        } catch (Exception e) {
            String errorMessage = e.getMessage();
            Boolean validError = errorMessage.contains('You cannot edit XXXX Products and Services record.');
            System.assertEquals(true, validError, 'Validation rule should throw correct error.');
        }

        System.runAs(u){
            product.SAP_Cust_No_Type_ID__c  = '2';
        }
        
       // Test edit of Version
        try {
            System.runAs(u) {
                product.Name = 'Cornerstone';
                product.Version__c = '1';
                update product;
        }
            throw new TestException('Failed to throw correctly');
        } catch (Exception e) {
            String errorMessage = e.getMessage();
            Boolean validError = errorMessage.contains('You cannot edit XXXX Products and Services record.');
            System.assertEquals(true, validError, 'Validation rule should throw correct error.');
        }

        System.runAs(u){
            product.Version__c  = '2';
        }
        
        // Test edit of PIMS Poeple ID 
        try {
            System.runAs(u) {
                product.Name = 'Cornerstone';
                product.PIMS_People_ID__c = 122222;
                update product;
        }
            throw new TestException('Failed to throw correctly');
        } catch (Exception e) {
            String errorMessage = e.getMessage();
            Boolean validError = errorMessage.contains('You cannot edit XXXX Products and Services record.');
            System.assertEquals(true, validError, 'Validation rule should throw correct error.');
        }

        System.runAs(u){
            product.PIMS_People_ID__c = 122222;
        }
        
       // Test edit of Account Id 
        try {
            System.runAs(u) {
                product.Name = 'Cornerstone';
                product.AccountId = 'ABC Clinic';
                update product;
        }
            throw new TestException('Failed to throw correctly');
        } catch (Exception e) {
            String errorMessage = e.getMessage();
            Boolean validError = errorMessage.contains('You cannot edit XXXX Products and Services record.');
            System.assertEquals(true, validError, 'Validation rule should throw correct error.');
        }

        System.runAs(u){
            product.AccountId = 'ABC Test Clinic';
        }

 
Abhishek BansalAbhishek Bansal
Hi Kasia,

You can add a debug after this line:
String errorMessage = e.getMessage();
system.debug(errorMessage);
Now check in the logs what error message you are getting, may be there is some other validation that might exist before the one that you are looking for.
Based on the debug log result you can modify your assert results accordingly.
Let me know if you need more help on this.

Thanks,
Abhishek Bansal.
Gmail: abhibansal2790@gmail.com
Skype: abhishek.bansal2790
Phone: +917357512102
Kallu KaliaKallu Kalia
Keep it up!