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
harish reddy 50harish reddy 50 

How to write testclass for trigger

Below trigger counts no of contacts and updates on noofcontacts__c on account .Now i need to write testclass for this 
As i am new to apex can someone help me with this .
Thanks in advance.


CODE:
trigger countofcontacttrigger on Contact (after insert) {

LIST<ID> ids=new LIST<ID>();

for(contact conn:trigger.new){
    

     ids.add(conn.accountid);
}

LIST<ACCOUNT> acc=[SELECT noofcontacts__c,(SELECT id from contacts) from account where id in:ids];
LIST<ACCOUNT> acc1=new LIST<ACCOUNT>();
for(account acc2:acc){

    acc2.noofcontacts__c=acc2.contacts.size();
    acc1.add(acc2);


}
update acc1;

}
Best Answer chosen by harish reddy 50
Shruti SShruti S
Here is the test class for your trigger - 
@isTest
private class CountOfConTriggerTest {
    private static final Integer NO_OF_RECORDS  = 100;
    private static final String ACCOUNT_NAME    = 'Acc_Test';

    private static void createTestData() {
        Account acc     = new Account();
        acc.Name        = ACCOUNT_NAME;
        INSERT acc;
        
        List<Contact> conList = new List<Contact>();
        
        for( Integer i = 1; i <= NO_OF_RECORDS; i++ ) {
            Contact con     = new Contact();
            con.LastName    = 'Con_Test';
            con.AccountId   = acc.Id;
            conList.add( con );
        }

        INSERT conList;
    }

    private static testMethod void trigger_Test() {
        createTestData();

        Account acc = [
            SELECT  Name
                    ,noofcontacts__c
            FROM    Account
            WHERE   Name = :ACCOUNT_NAME
        ];

        System.assertEquals( NO_OF_RECORDS, acc.noofcontacts__c );
    }
}
Please let me know if you have any more doubts.

All Answers

Shruti SShruti S
Here is the test class for your trigger - 
@isTest
private class CountOfConTriggerTest {
    private static final Integer NO_OF_RECORDS  = 100;
    private static final String ACCOUNT_NAME    = 'Acc_Test';

    private static void createTestData() {
        Account acc     = new Account();
        acc.Name        = ACCOUNT_NAME;
        INSERT acc;
        
        List<Contact> conList = new List<Contact>();
        
        for( Integer i = 1; i <= NO_OF_RECORDS; i++ ) {
            Contact con     = new Contact();
            con.LastName    = 'Con_Test';
            con.AccountId   = acc.Id;
            conList.add( con );
        }

        INSERT conList;
    }

    private static testMethod void trigger_Test() {
        createTestData();

        Account acc = [
            SELECT  Name
                    ,noofcontacts__c
            FROM    Account
            WHERE   Name = :ACCOUNT_NAME
        ];

        System.assertEquals( NO_OF_RECORDS, acc.noofcontacts__c );
    }
}
Please let me know if you have any more doubts.
This was selected as the best answer
harish reddy 50harish reddy 50
Tnx shruthi for your reply .why we are assigning 100 to noof records and what does assertequals do here...?
Shruti SShruti S
I tested on 100 records to do stress testing. And we always require System.assertEquals in a Test Method. Here the System.assertEquals checks if the noofcontacts__c field on the Account contains 100 as that field indicates the number of contacts associated with that account. As you can see, we had already created 100 contact records and associated them with the Account record.