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
Mitesh Saparia 8Mitesh Saparia 8 

How to write test class for trigger?

trigger conlists on Account (before insert) {
    if(trigger.isInsert && trigger.isAfter)
    {
        List<Contact> lstCon = new List<Contact>();
        for(Account acc : trigger.new)
        {
            Contact con = new Contact();
            con.lastName=acc.Name;
            con.AccountId = acc.Id;
            lstCon.add(con);
        }
        insert(lstCon);
    }
    if(trigger.isUpdate && trigger.isAfter)
    {
        Map<Id,Account> mapACC = new Map<Id,Account>([select (select id,name from contacts)
                                                      from account where id IN: trigger.new]);
        List<Contact> newCon = new List<Contact>();
        for(Account updatedAcc : trigger.New)
        {
            Account oldAcc = trigger.oldMap.get(updatedAcc.Id);
            if(mapACC.ContainsKEY(updatedAcc.Id) && oldAcc.Name != updatedAcc.Name)
            {
                for(Contact con : mapACC.get(updatedAcc.Id).contacts)
                {
                    con.lastname = updatedAcc.Name;
                    newCon.add(con);
                }
            }
        }
        update newCon;
    }
}
Best Answer chosen by Mitesh Saparia 8
Manish ArvindManish Arvind
Hi Mitesh,
Greetings to you!

You can write test class as below : 

@isTest public class AccountTriggerTest {

    @isTest public static void testInsertAccount(){
     Test.startTest();
     Account acNewOne = new Account(Name = 'Test');
     insert acNewOne;
     Contact con = [SELECT Id, LastName FROM Contact WHERE AccountId =: acNewOne.Id];
     System.assertNotEqual(null, con);
     System.assertEqual('Test', con.LastName);
     Test.stopTest();
  }
        
    @isTest public static void testUpdateAccount(){
     Test.startTest();
     Account acNewOne = new Account(Name = 'Test');
     insert acNewOne;
     acNewOne.Name = 'Test2';
     update acNewOne;
     Contact con = [SELECT Id, LastName FROM Contact WHERE AccountId =: acNewOne.Id];
     System.assertNotEqual(null, con);
     System.assertEqual('Test2', con.LastName);
    Test.stopTest();
    }
}
    
One more thing in your code, I am not sure that your code will work. Since :
1) in trigger definition, you are using parameter as 'before insert'. So trigger will be executed for before insert.
2) in trigger code, you are using 'if blocks' for Trigger Context Variable 'trigger.isAfter' or/and 'trigger.isUpdate'. So if block will be executed if trigger is called on any Account Update or AFTER event. 
Please check it. Rest test class will be working.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Manish Arvind

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Mitesh,

Greetings to you!

As you are using after insert and after update in your code, change your trigger to:
trigger conlists on Account (after insert, after update)

Below is the test class with 100% code coverage:
@isTest
public class Test_conlistsTrigger {

    public static testMethod void test1() {
        
        // Insert Account
        Account acc = new Account();
        acc.Name = 'Khan_Test';
        INSERT acc;
        
        // Update Account
        acc.Name = 'Test_Update';
        UPDATE acc;
        
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Mitesh Saparia 8Mitesh Saparia 8
Not Working this 
Ajay K DubediAjay K Dubedi
Hi Mitesh,
Your trigger has some mistakes, so follow the following trigger and its test class:
Trigger:
trigger conlists on Account (before insert,before update) {
    if(trigger.isInsert && trigger.isbefore)
    {
        List<Contact> lstCon = new List<Contact>();
        for(Account acc : trigger.new)
        {
            Contact con = new Contact();
            con.lastName=acc.Name;
            con.AccountId = acc.Id;
            lstCon.add(con);
        }
        insert(lstCon);
    }
    if(trigger.isUpdate && trigger.isbefore)
    {
        Map<Id,Account> mapACC = new Map<Id,Account>([select (select id,name from contacts)
                                                      from account where id IN: trigger.new]);
        List<Contact> newCon = new List<Contact>();
        for(Account updatedAcc : trigger.New)
        {
            Account oldAcc = trigger.oldMap.get(updatedAcc.Id);
            if(mapACC.ContainsKEY(updatedAcc.Id) && oldAcc.Name != updatedAcc.Name)
            {
                for(Contact con : mapACC.get(updatedAcc.Id).contacts)
                {
                    con.lastname = updatedAcc.Name;
                    newCon.add(con);
                }
            }
        }
        update newCon;
    }
}
Test-Class:
@IsTest
public class conlistsTest {
    @IsTest
    public static void conlistTest(){
        Account acc=new Account();
        acc.Name='test1';
        acc.Email__c='test1a@gmail.com';
        insert acc;
        acc.Name='test1';
        update acc;
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Manish ArvindManish Arvind
Hi Mitesh,
Greetings to you!

You can write test class as below : 

@isTest public class AccountTriggerTest {

    @isTest public static void testInsertAccount(){
     Test.startTest();
     Account acNewOne = new Account(Name = 'Test');
     insert acNewOne;
     Contact con = [SELECT Id, LastName FROM Contact WHERE AccountId =: acNewOne.Id];
     System.assertNotEqual(null, con);
     System.assertEqual('Test', con.LastName);
     Test.stopTest();
  }
        
    @isTest public static void testUpdateAccount(){
     Test.startTest();
     Account acNewOne = new Account(Name = 'Test');
     insert acNewOne;
     acNewOne.Name = 'Test2';
     update acNewOne;
     Contact con = [SELECT Id, LastName FROM Contact WHERE AccountId =: acNewOne.Id];
     System.assertNotEqual(null, con);
     System.assertEqual('Test2', con.LastName);
    Test.stopTest();
    }
}
    
One more thing in your code, I am not sure that your code will work. Since :
1) in trigger definition, you are using parameter as 'before insert'. So trigger will be executed for before insert.
2) in trigger code, you are using 'if blocks' for Trigger Context Variable 'trigger.isAfter' or/and 'trigger.isUpdate'. So if block will be executed if trigger is called on any Account Update or AFTER event. 
Please check it. Rest test class will be working.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Manish Arvind
This was selected as the best answer
Mitesh Saparia 8Mitesh Saparia 8
yes solved