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
Shruti VishShruti Vish 

Test class for trigger handler class

This is my class please help me out to write test class
public class AccountTriggerHandler implements ITriggerHandler
{
    public static Boolean TriggerDisabled = false;

    /*
        Checks to see if the trigger has been disabled. For example, you could check a custom setting here.
        In this example, a static property is used to disable the trigger.
        In a unit test, you could use AccountTriggerHandler.TriggerDisabled = true to completely disable the trigger.
    */
    public Boolean IsDisabled()
    {
        return TriggerDisabled;
    }

    public void BeforeInsert(List<SObject> newItems) 
    {
        // Cast the List<Sobject> to List<Account>
        AccountNameCheck((List<Account>)newitems);
    }

    public void BeforeUpdate(Map<Id, SObject> newItems, Map<Id, SObject> oldItems) 
    {
        // Cast the Map<Id, Sobject> to List<Account>
        AccountNameCheck((List<Account>)newitems.values());
    }

    public void BeforeDelete(Map<Id, SObject> oldItems) {}

    public void AfterInsert(Map<Id, SObject> newItems) {}

    public void AfterUpdate(Map<Id, SObject> newItems, Map<Id, SObject> oldItems) {}

    public void AfterDelete(Map<Id, SObject> oldItems) {}

    public void AfterUndelete(Map<Id, SObject> oldItems) {}

    /*
        Check the accounts to make sure their name does not contain the text "test".
        If any do, reject them.
    */
    private void AccountNameCheck(List<Account> accountList)
    {
        // Reject any Accounts which have the word "Test" in the name
        for (Account acc : accountList)
        {
            //if (acc.Name.contains('test'))
                //acc.Name.addError('You may not use the word "test" in the account name');
        }
    }
}
Amit Chaudhary 8Amit Chaudhary 8
Can you please post your trigger code as well from where you are calling above class.

For Trigger Test class you just need to create the account record only in your test class
@isTest 
public class TriggerTestClass 
{
    static testMethod void testMethod1() 
    {
     Account testAccount = new Account(); 
     testAccount.Name='Test Account' ; 
     insert testAccount;
    
    }
}