Write test class for trigger?-SimpleExample

How to Write test class for trigger:

Here is the example to write a unit test method for a simple Apex Trigger.
Following trigger is executed whenever account is created and creates sharing to the manager to that record.

Apex Trigger Code:
trigger accountAfterInsert on Account (after insert) {
string managerId= [Select Id, ManagerId FROM User WHERE Id = :userInfo.getUserId()].ManagerId;
for(Account acc: trigger.New){
AccountShare accShare = new AccountShare();
accShare .ParentId = acc.Id;
accShare .UserOrGroupId = managerId;
accShare .AccessLevel = ‘EDIT’;
accShare .RowCause = Schema.accountShare.RowCause.Manual;
}
}

// Unit Test for above Trigger
@isTest
private class AccountTriggersTest{
private static testmethod void accountTriggersTest(){
Account acc = new Account();
acc.name = ‘NewAccount’;
acc.address = ‘USA’;
insert acc;
}
}

One thought on “Write test class for trigger?-SimpleExample”

  1. Hi, I have all latest dumps of salesforce for all types of salesforce exams if anyone wants you can mail me at
    sfdcconsultant25@gmail.com

    These are original questions from the certification exam and very useful to pass the exam.
    I have all latest dumps of following exams
    90% questions come from it

    Salesforce Administrator (ADM 201)
    Salesforce Sales Cloud Consultant (CON 201)
    Salesforce Service Cloud Consultant
    Platform Developer I
    App Builder

    Immediately i will sent dumps latest one,which helps you to pass

    I will give you as-surety for all latest dumps

Leave a Reply