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
naveen reddy 68naveen reddy 68 

can some one help me writing test class for this program

trigger NumOfContacts on contact (after insert, after update, after delete, after undelete) 
{
    
    List<account> acc = new List<account>();
    
    Set<Id> sid = new Set<Id>();
    
    if(Trigger.isDelete) 
    {
        for(contact con:Trigger.Old) 
        {
            sid.add(con.accountId);   
        }    
    }
    else
        if(Trigger.isUpdate) 
    {
        
        for(contact con:Trigger.New) 
        {            
            sid.add(con.accountId);   
        }
        
        for(contact con:Trigger.Old)
        {
            sid.add(con.accountId);   
        }   
    }
    
    else
    {
        for(contact con:Trigger.New) 
        {
            sid.add(con.accountId);   
        }
    }
    
    AggregateResult[] groupedResults = [SELECT COUNT(Id),accountId FROM contact where accountID IN :sid GROUP BY accountID ];
    
    for(AggregateResult ar:groupedResults) 
    {
        
        Id custid = (ID)ar.get('accountId');
        
        Integer count = (INTEGER)ar.get('expr0');
        
        account cust1 = new account(Id=custid);
        
        cust1.No_Of_Contacts__c = count;
        
        acc.add(cust1);
        
    }
    
    
    update acc;
    
}
Best Answer chosen by naveen reddy 68
Saurabh BSaurabh B
Hi Naveen, try this... It will give you 100% test coverage. It has been tested in dev org ...
 
@isTest 
private class NumOfContacts_TEST {
    static testMethod void NumOfContactsTEST() {
       Account A = new Account(Name='Test Account');
       insert A ;
    

		Contact C1 = New Contact (FirstName='Test1',LastName='Test1',Accountid=A.id);
		insert C1;
        
        Contact C2 = New Contact (FirstName='Test2',LastName='Test2',Accountid=A.id);
		insert C2;
        
        A = [SELECT ID,No_Of_Contacts__c FROM Account WHERE ID = :A.Id];
        
               System.assertEquals(2, A.No_Of_Contacts__c);
        
       	 
       	C2 = [SELECT ID From Contact WHERE ID = :C2.Id];
        Delete C2;
                       
        A = [SELECT ID,No_Of_Contacts__c FROM Account WHERE ID = :A.Id];
        
               System.assertEquals(1, A.No_Of_Contacts__c);
		update c1;
    }
}

Please mark this as BEST ANSWER if it help!

All Answers

Deepak Pandey 13Deepak Pandey 13
@isTest
public class TestAccountActive 
  {
     static testmethod void testme(){
         account a = new account();
         a.name = 'Test Name';   
         insert a;
         list<contact> lstcon = new list<>(Contact);
         contact c =new contact(lastname = 'siva',Active__c = false,accountid = a.id);
         lstcon.add(c);
         
        contact c1 =new contact(lastname = 'siva',Active__c = false,accountid = a.id);     
        lstcon.add(c1);        
          insert lstcon;  
    update  lstcon;
         }
  }
Vijaya AmarnathVijaya Amarnath
@isTest
public class NumOfContacts_Test
{
	static testMethod void NumCons()
	{
		Account a = new Account();
		 a.Name = 'Test Acc';
		insert a;

		Contact c = new Contact();
		 c.LastName = 'Con 1';
		 c.AccountId = a.Id;
		insert c;

		Test.StartTest();
			Update a;
		Test.StopTest();
	}
}

Try this..
Saurabh BSaurabh B
Hi Naveen, try this... It will give you 100% test coverage. It has been tested in dev org ...
 
@isTest 
private class NumOfContacts_TEST {
    static testMethod void NumOfContactsTEST() {
       Account A = new Account(Name='Test Account');
       insert A ;
    

		Contact C1 = New Contact (FirstName='Test1',LastName='Test1',Accountid=A.id);
		insert C1;
        
        Contact C2 = New Contact (FirstName='Test2',LastName='Test2',Accountid=A.id);
		insert C2;
        
        A = [SELECT ID,No_Of_Contacts__c FROM Account WHERE ID = :A.Id];
        
               System.assertEquals(2, A.No_Of_Contacts__c);
        
       	 
       	C2 = [SELECT ID From Contact WHERE ID = :C2.Id];
        Delete C2;
                       
        A = [SELECT ID,No_Of_Contacts__c FROM Account WHERE ID = :A.Id];
        
               System.assertEquals(1, A.No_Of_Contacts__c);
		update c1;
    }
}

Please mark this as BEST ANSWER if it help!
This was selected as the best answer