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
Abhishek Sharma 527Abhishek Sharma 527 

Test Case for custom roll up summary

Hello There, I have custom roll up summary handler class and trigger created seperately, I'm not aware about testing and don't know whether it will be written for handler class or trigger program. 

also can anyone help in adding test case for this insertMethod (I have 2 other method, for which I will implement with that reference )

//handler class
public class Contact_count_Handler 
{     
    public static void InsertMethod(List<Contact> newList){
        List<Account> accList = new List<Account>();
        try{
        Set<id> accountIds = new Set<id>();        
        
        if(newList!=null) {
            
            for(contact c: newList)    
            {
                if(c.accountId!=null)            
                {
                    accountIds.add(c.accountId); 
                }
            }
           
            
            if(!accountIds.isEmpty()){
                for(Account acc :[Select id,Number_of_linked_contact__c,(Select id,name from contacts) from Account where Id in : accountIds]){
                    
                    acc.Number_of_linked_contact__c= acc.contacts.size();
                    
                    acclist.add(acc);
                }
                if(acclist.size()>0){
                    update accList; 
                }
            }
        }   
        
    }
         catch(Exception e){
            System.debug('insert method execution under catch block');
        }
    }
    }

//trigger code
trigger Contact_count_trigger on Contact (after insert, after update, after delete) { 

    if(trigger.isAfter && Trigger.isInsert){ 
        Contact_count_Handler.InsertMethod(trigger.new);
}
}
Best Answer chosen by Abhishek Sharma 527
AnkaiahAnkaiah (Salesforce Developers) 
Hi Abhishek,

Find the below answers.
1. If you get too many soql rows then use the @StartTesrt & @Stoptest. otherwise no need.
for more infomation refer the below link.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_start_stop_test.htm
2. Triggers fires whenever DML occurs on object, If any trigger are there for the partcular object then it will fire and it will pass the data to the respective apex class /trigger  usring the trigger context variables like trigger.New & trigger.Old,Trigger.Newmap & trigger.oldmap.
3. Contact_count_trigger_Test should cover the code coverage for trigger & apex class.

Refer the below trailhead will help you, how write a test class.
https://trailhead.salesforce.com/content/learn/modules/apex_testing/apex_testing_triggers

If the above information helps, Please mark it as best answer.

Thanks!!

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Abhishek,

try with below code.
@isTest
private class Contact_count_trigger_Test {
    static testMethod void testtwoCon(){
        
        Account a = new Account(name='test acc',phone='12345667');
        insert a;
        Contact con = new Contact(accountid=a.id,lastname='test con',email='lnarasimha823@gmail.com');
        insert con;
		Contact con2 = new Contact(accountid=a.id,lastname='test con1',email='lnarasimha82@gmail.com');
        insert con2;
		
		}
}

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​
 
CharuDuttCharuDutt
Hii Abhishek
Try Below Test Class
@isTest
public class NumberOfChildTest {
    @isTest
    public Static Void UnitTest(){
        list<Contact>lstCon = new list<Contact>();
        Account Acc = new account();
        Acc.name ='test';
        insert Acc;
        
        Account Acc2 = new account();
        Acc2.name ='test2';
        insert Acc2;
        
      
        
        Contact Con = new Contact();
        Con.LastName = 'TestCon';
        Con.AccountId = Acc.Id;
        Insert Con;
       
        Con.AccountId = Acc2.Id;
        Update Con;
        Delete Con;
        
        
        
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
Abhishek Sharma 527Abhishek Sharma 527
 Thanks for response ankaiah and Charu,
I) I have to add @start and @finish before insert con, is it? 
II) and I have doubt, how it is getting linked with that handler class. I mean how we are confirming that it is test class for that particular apex program. maybe it's dumb question, plz guide
III) and do I have to write test case for trigger also (for this one)
trigger code
trigger Contact_count_trigger on Contact (after insert, after update, after delete) { 

    if(trigger.isAfter && Trigger.isInsert){ 
        Contact_count_Handler.InsertMethod(trigger.new);
}
}
AnkaiahAnkaiah (Salesforce Developers) 
Hi Abhishek,

Find the below answers.
1. If you get too many soql rows then use the @StartTesrt & @Stoptest. otherwise no need.
for more infomation refer the below link.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_start_stop_test.htm
2. Triggers fires whenever DML occurs on object, If any trigger are there for the partcular object then it will fire and it will pass the data to the respective apex class /trigger  usring the trigger context variables like trigger.New & trigger.Old,Trigger.Newmap & trigger.oldmap.
3. Contact_count_trigger_Test should cover the code coverage for trigger & apex class.

Refer the below trailhead will help you, how write a test class.
https://trailhead.salesforce.com/content/learn/modules/apex_testing/apex_testing_triggers

If the above information helps, Please mark it as best answer.

Thanks!!
This was selected as the best answer