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
CBNCBN 

Help me to Write Test Class for below Trigger using @Testsetup

trigger countContact on Contact (after insert, after update, after delete, after undelete)
{
  Set<Id> setAccountIds = new Set<Id>();
 
  //Whenever your working with After Undelete operation you can access data through
  //Trigger.new or Trigger.newMap but not with Trigger.old or Trigger.oldmap variables
  if(Trigger.isInsert || Trigger.isUndelete || Trigger.isUpdate)
  {
   for(Contact con : Trigger.new)
   {
    setAccountIds.add(con.AccountId);
   }
  }
 
  if(Trigger.isDelete)
  {
   //if you use Trigger.new below in place of Trigger.old you will end up with
   //System.NullPointerException:Attempt to de-reference a null object
   for(Contact con : Trigger.old)
   {
    setAccountIds.add(con.AccountId);
   }
  }
 
 List<Account> listAccs = [Select id,name,Total_No_Of_Contacts__c ,(Select id from contacts) from Account where Id in : setAccountIds];
  for(Account acc :listAccs)
  {
   acc.Total_No_Of_Contacts__c = acc.contacts.size();
  }
          List<Account> listAccs1 = [Select id,name,No_of_active_contacts__c ,(select id from contacts where Contact_Roll__c = True) from Account where Id in : setAccountIds];
  for(Account acc1 :listAccs1)
  {
   acc1.No_of_active_contacts__c = acc1.contacts.size();
  }
  update listAccs;
  update listAccs1;
 
}
 
Best Answer chosen by CBN
Raj VakatiRaj Vakati
use this
@isTest
private class countContactTest {

    @testSetup static void setup() {
		Account a = new Account(name='test acc',phone='9494146144');
        insert a;
        Account a2 = new Account(name='test acc 2',phone='9494146144');
        insert a2;
        Contact con = new Contact(accountid=a.id,lastname='test con',email='jasdhjga@gmail.com');
        insert con;
        Contact con2 = new Contact(accountid=a2.id,lastname='test con2',email='sdghagsd@gmail.com');
	insert con2 ; 
		
    }
    
    @isTest static void testMethod1() {
        Account acct = [SELECT Id FROM Account WHERE Name='test acc' LIMIT 1];
        Contact cont = [SELECT Id,AccountId FROM Contact WHERE LastName='test con2' LIMIT 1];
		cont.AccountId = acct.Id ; 
		Update cont ; 
	   
	 }

	 @isTest static void testMethod2() {
        Account acct = [SELECT Id FROM Account WHERE Name='test acc' LIMIT 1];
        Contact cont = [SELECT Id,AccountId FROM Contact WHERE LastName='test con2' LIMIT 1];
		delete cont ; 
	   undelete cont;

	 }
}

 

All Answers

Raj VakatiRaj Vakati
use this
@isTest
private class countContactTest {

    @testSetup static void setup() {
		Account a = new Account(name='test acc',phone='9494146144');
        insert a;
        Account a2 = new Account(name='test acc 2',phone='9494146144');
        insert a2;
        Contact con = new Contact(accountid=a.id,lastname='test con',email='jasdhjga@gmail.com');
        insert con;
        Contact con2 = new Contact(accountid=a2.id,lastname='test con2',email='sdghagsd@gmail.com');
	insert con2 ; 
		
    }
    
    @isTest static void testMethod1() {
        Account acct = [SELECT Id FROM Account WHERE Name='test acc' LIMIT 1];
        Contact cont = [SELECT Id,AccountId FROM Contact WHERE LastName='test con2' LIMIT 1];
		cont.AccountId = acct.Id ; 
		Update cont ; 
	   
	 }

	 @isTest static void testMethod2() {
        Account acct = [SELECT Id FROM Account WHERE Name='test acc' LIMIT 1];
        Contact cont = [SELECT Id,AccountId FROM Contact WHERE LastName='test con2' LIMIT 1];
		delete cont ; 
	   undelete cont;

	 }
}

 
This was selected as the best answer
Umesh RathiUmesh Rathi
Hi,

I haven't Tested it but you can try something like this
@isTestst
private class TestClass {

static testMethod void countContactTest(){
Account acc = new Account();
acc.name = 'Test Account';
insert acc;

Contact con1 = new Contact();
con1.lastName = 'Test1 Con';
con.accountId = acc.id;
con.Contact_Roll__c = true;

Contact con1 = new Contact();
con1.lastName = 'Test2 Con';
con.accountId = acc.id;
con.Contact_Roll__c = false;

Test.startTest();
insert con1;
insert con2;
Test.stopTest();

System.assertEquals(2,acc.Total_No_Of_Contacts__c);
System.assertEquals(1,acc.No_of_active_contacts__c);
}
}

Please mark this answer as correct if it solves your Purpose.​​​​​​​ 
Thanks!