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 Kannoju02547126464965992Naveen Kannoju02547126464965992 

write a test class for this Trigger,

please give help for this.

trigger countOnContact on Contact(after insert, after delete)
{
Set<id> accid=new Set<id>();

List<Contact> conlist=new List<Contact>();
List<Contact> listcon=new List<Contact>();

List<Account> acclist=new List<Account>();
List<Account> listacc=new List<Account>();

Map<id ,integer> mapCount=new Map<id,integer>();
   
   if(trigger.isInsert)
   {
      for(Contact con:trigger.new)
      {
      accid.add(con.Accountid);
      }
   }
    if(trigger.isdelete)
   {
      for(Contact con:trigger.old)
      {
      accid.add(con.Accountid);
      }
   }


acclist=[select id,name from Account where id in:accid];
conlist=[select id,name, accountid from Contact where accountid in:accid];
  
   for(Account acc:acclist)
   {
   listcon.clear();
   for(contact c: conlist)
     {
      if(c.accountid==acc.id)
       {
         listcon.add(c);
         mapCount.put(c.accountid,listcon.size());
       }
     }
   }
if(acclist.size()>0)
{
  for(Account a: acclist)
  {
  if(mapCount.get(a.id)==null)
  a.CountOfContacts__c=0;
  else
  a.CountOfContacts__c=mapCount.get(a.id);
  listacc.add(a);
  }

if(listacc.size()>0)
update listacc;
}
}

Thanks
Naveen
John PipkinJohn Pipkin
Here ya go:
 
public static testMethod test(){

	List<Account> alist = new List<Account>();

	Account a1 = new Account(Name='Test Account 1');
	alist.add(a1);
	Account a2 = new Account(Name='Test Account 2');
	alist.add(a2);

	insert alist;

	List<Contact> clist = new List<Contact>();

	Contact c1 = new Contact(LastName='test1',AccountId = a1.Id);
	clist.add(c1);
	Contact c2 = new Contact(LastName='test2',AccountId = a1.Id);
	clist.add(c2);
	Contact c3 = new Contact(LastName='test3',AccountId = a2.Id);
	clist.add(c3);
	Contact c4 = new Contact(LastName='test4',AccountId = a2.Id);
	clist.add(c4);
	Contact c5 = new Contact(LastName='test5',AccountId = a2.Id);
	clist.add(c5);

	test.startTest();

	insert clist;

	Account upd_a1 = [Select Id, CountOfContacts__c from Account where Id = :a1.Id];
	system.assertEquals(2,upd_a1.CountOfContacts__c);
	Account upd_s2 = [Select Id, CountOfContacts__c from Account where Id = :a2.Id];
	system.assertEquals(3,upd_a2.CountOfContacts__c);

	List<Contact> deleteList = new List<Contact>();
	deleteList.add(c1);
	deleteList.add(c3);

	delete deleteList;

	test.stopTest();

	Account upd_a3 = [Select Id, CountOfContacts__c from Account where Id = :a1.Id];
	system.assertEquals(1,upd_a3.CountOfContacts__c);
	Account upd_s4 = [Select Id, CountOfContacts__c from Account where Id = :a2.Id];
	system.assertEquals(2,upd_a4.CountOfContacts__c);

}

 
John PipkinJohn Pipkin
oops. forgot a word in the first line...
 
public static testMethod void test()

 
Naveen Kannoju02547126464965992Naveen Kannoju02547126464965992
Hi...John,

Thanks for given this reply.

and one more  thing is this code gives 96% code coverage.
this line will not cover 49. a.CountOfContacts__c=0;

Thanks
Naveen
John PipkinJohn Pipkin
I overlooked that. All you would need to do is create one more account (with no contacts associated with it) and do an assert with 0 as the expected value. Like this: 
 
public static testMethod void test(){

	List<Account> alist = new List<Account>();

	Account a1 = new Account(Name='Test Account 1');
	alist.add(a1);
	Account a2 = new Account(Name='Test Account 2');
	alist.add(a2);
//new account with no contact
	Account a3 = new Account(Name='Test Account 3');
	alist.add(a3);

	insert alist;

	List<Contact> clist = new List<Contact>();

	Contact c1 = new Contact(LastName='test1',AccountId = a1.Id);
	clist.add(c1);
	Contact c2 = new Contact(LastName='test2',AccountId = a1.Id);
	clist.add(c2);
	Contact c3 = new Contact(LastName='test3',AccountId = a2.Id);
	clist.add(c3);
	Contact c4 = new Contact(LastName='test4',AccountId = a2.Id);
	clist.add(c4);
	Contact c5 = new Contact(LastName='test5',AccountId = a2.Id);
	clist.add(c5);

	test.startTest();

	insert clist;

	Account upd_a1 = [Select Id, CountOfContacts__c from Account where Id = :a1.Id];
	system.assertEquals(2,upd_a1.CountOfContacts__c);
	Account upd_s2 = [Select Id, CountOfContacts__c from Account where Id = :a2.Id];
	system.assertEquals(3,upd_a2.CountOfContacts__c);
//assert 0
	Account upd_s2_1 = [Select Id, CountOfContacts__c from Account where Id = :a3.Id];
	system.assertEquals(0,upd_s2_1.CountOfContacts__c);

	List<Contact> deleteList = new List<Contact>();
	deleteList.add(c1);
	deleteList.add(c3);

	delete deleteList;

	test.stopTest();

	Account upd_a3 = [Select Id, CountOfContacts__c from Account where Id = :a1.Id];
	system.assertEquals(1,upd_a3.CountOfContacts__c);
	Account upd_s4 = [Select Id, CountOfContacts__c from Account where Id = :a2.Id];
	system.assertEquals(2,upd_a4.CountOfContacts__c);

}

 
Naveen Kannoju02547126464965992Naveen Kannoju02547126464965992
Hi..John

Thank you very much for given to this reply. It is very useful to me.
if you don't mind could you please give me your mailID.

Thanks,
Naveen.
John PipkinJohn Pipkin
I'm glad that helped you out. My email is john.pipkin@nextstepliving.com. 

Thanks
Naveen Kannoju02547126464965992Naveen Kannoju02547126464965992
Thank you john

Thanks,
Naveen