You need to sign in to do that
Don't have an account?
Ramana123
how to cover the test class for below trigger
Highlighed part is not covering in the test class--- Helper class
public class TriggerHelperClass {
public Set <Id> accountIds = new Set <Id>();
public List <Account> lstAccountsToUpdate = new List<Account>();
// for insertion and deletion
public void insertAndUndelete(List<Contact> newAcc)
{
for(Contact con:newAcc)
{
accountIds.add(con.accountID);
}
this.updatingContactsCount();
}
// for delete operation
public void deleteOperation(List<Contact> newAcc)
{
for(Contact con:newAcc)
{
accountIds.add(con.accountID);
}
this.updatingContactsCount();
}
public void updateOperation(List<Contact> newAcc,Map<Id,Contact>oldMap)
{
for(Contact con : newAcc)
{
if(con.AccountId != null)
{
if(oldMap.get(con.Id).AccountId != con.AccountId)
{
accountIds.add(con.AccountId);
}
}
accountIds.add(oldMap.get(con.Id).AccountId);
}
this.updatingContactsCount();
}
Public void updatingContactsCount()
{
for(Account acc:[SELECT Id,Name,Count_Contacts__c,Total_Sum__c,(Select Id,Sum__c from Contacts) from Account where Id IN: accountIds])
{
acc.Count_Contacts__c = acc.Contacts.size();
acc.Total_Sum__c = 0;
for(Contact con : acc.Contacts)
{
if(con.Sum__c != NULL)
{
acc.Total_Sum__c = acc.Total_Sum__c + con.Sum__c;
}
}
lstAccountsToUpdate.add(acc);
}
UPDATE lstAccountsToUpdate;
}
}
test class
@isTest
private class UpdatingAccountRelatedContactsCount_Test {
static Testmethod void accountRelatedContacts()
{
List<Contact> conList = new List<Contact>();
Account Acc = New Account();
Acc.Name = 'srikanth';
insert Acc;
Contact con = new Contact();
con.LastName = 'Test';
con.AccountId = Acc.Id;
con.Sum__c = 100;
conList.add(con);
Contact con1 = new Contact();
con.LastName = 'Test1';
con1.AccountId = Acc.Id;
con1.Sum__c = 100;
conList.add(con1);
insert conList;
delete con1;
Test.StartTest();
TriggerHelperClass obj = new TriggerHelperClass();
update conList ;
undelete con1;
Test.StopTest();
}
}
public class TriggerHelperClass {
public Set <Id> accountIds = new Set <Id>();
public List <Account> lstAccountsToUpdate = new List<Account>();
// for insertion and deletion
public void insertAndUndelete(List<Contact> newAcc)
{
for(Contact con:newAcc)
{
accountIds.add(con.accountID);
}
this.updatingContactsCount();
}
// for delete operation
public void deleteOperation(List<Contact> newAcc)
{
for(Contact con:newAcc)
{
accountIds.add(con.accountID);
}
this.updatingContactsCount();
}
public void updateOperation(List<Contact> newAcc,Map<Id,Contact>oldMap)
{
for(Contact con : newAcc)
{
if(con.AccountId != null)
{
if(oldMap.get(con.Id).AccountId != con.AccountId)
{
accountIds.add(con.AccountId);
}
}
accountIds.add(oldMap.get(con.Id).AccountId);
}
this.updatingContactsCount();
}
Public void updatingContactsCount()
{
for(Account acc:[SELECT Id,Name,Count_Contacts__c,Total_Sum__c,(Select Id,Sum__c from Contacts) from Account where Id IN: accountIds])
{
acc.Count_Contacts__c = acc.Contacts.size();
acc.Total_Sum__c = 0;
for(Contact con : acc.Contacts)
{
if(con.Sum__c != NULL)
{
acc.Total_Sum__c = acc.Total_Sum__c + con.Sum__c;
}
}
lstAccountsToUpdate.add(acc);
}
UPDATE lstAccountsToUpdate;
}
}
test class
@isTest
private class UpdatingAccountRelatedContactsCount_Test {
static Testmethod void accountRelatedContacts()
{
List<Contact> conList = new List<Contact>();
Account Acc = New Account();
Acc.Name = 'srikanth';
insert Acc;
Contact con = new Contact();
con.LastName = 'Test';
con.AccountId = Acc.Id;
con.Sum__c = 100;
conList.add(con);
Contact con1 = new Contact();
con.LastName = 'Test1';
con1.AccountId = Acc.Id;
con1.Sum__c = 100;
conList.add(con1);
insert conList;
delete con1;
Test.StartTest();
TriggerHelperClass obj = new TriggerHelperClass();
update conList ;
undelete con1;
Test.StopTest();
}
}
Can you please try adding the following lines to your test class?
Let me know if this helps, if it does, please mark this answer as best so that others facing the same issue will find this information useful. Thank you
Let me know if this fixes your issue and gives you any coverage?