apexapex-trigger

Test class for Apex Trigger


I am new in Apex Development.

I want to write a TestClass for my Apex Trigger.

I am sharing my code with you.

trigger UpdateContact on Account (after Insert) {

    List <Contact> contactList=new List<Contact>();

    for(Account a:Trigger.new)
    {
        Contact c=new Contact(LastName=a.Name, AccountId=a.id);
        contactList.add(c);
    }

    insert contactList;

}

Solution

  • Since your trigger in on Account (after insert), provided that there is no other mandatory field or validation rule, just simply insert an account to test your trigger:

    @isTest
    private class UpdateContactTest 
    {
        static testMethod void myUnitTest()
        {
            Account acc = new Account(
                Name = 'Test');
    
            insert acc;
        }
    }