Test class for Apex Trigger

160 Views Asked by At

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;

}
1

There are 1 best solutions below

0
On BEST ANSWER

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;
    }
}