You need to sign in to do that
Don't have an account?

Test class code coverage issue
Hi Friends,
I have code coverage issue while writing a test class for the trigger. One line is not passed the code coverage I did not find the reason, why it is not .Can any one suggest me the reason. I am getting 87% code coverage.
Trigger:
trigger AccountsProcessorTrig on Account (after insert) {
if(trigger.isInsert){
AccountProcessor.countContacts(trigger.newMap.keySet());
}
}
Apex Class:
public class AccountProcessor {
public static integer count=0;
@future
public static void countContacts(set<Id> accountids){
List<Account> accountstobulkify=[SELECT Id, Name FROM Account WHERE Id IN:accountids];
Map<Id,Account> accountstoupdate=new Map<Id,Account>();
for(Account a:accountstobulkify){
for(Contact c:a.Contacts){
count=count++;// this line is not passed
}
a.Number_Of_Contacts__c=count;
}
}
}
Test class:
@isTest
private class AccountProcessorTest {
@isTest
static void CountNoOfContacts(){
Account testacc=new Account(Name='Numberofcontactstest',Industry='Finance');
insert testacc;
List<Contact> testlist=new List<Contact>();
for(integer i=0; i<10; i++){
Contact testcon=new Contact(LastName='AccountContact'+i, AccountId=testacc.id);
testlist.add(testcon);
}
insert testlist;
system.debug('size:'+testlist.size());
Map<Id,Account> testids=new Map<Id,Account>([SELECT Id, Name, Number_Of_Contacts__c FROM Account WHERE Name=:'Numberofcontactstest']);
Set<Id> accids=testids.keySet();
system.debug('Contcts:'+([SELECT Count() FROM Contact WHERE AccountId=:testacc.Id]));
test.startTest();
AccountProcessor.countContacts(accids);
test.stopTest();
decimal a=[SELECT ID, Name FROM Account WHERE Name=:'Numberofcontactstest'].Number_Of_Contacts__c;
system.assertEquals(10, a);
}
}
I have code coverage issue while writing a test class for the trigger. One line is not passed the code coverage I did not find the reason, why it is not .Can any one suggest me the reason. I am getting 87% code coverage.
Trigger:
trigger AccountsProcessorTrig on Account (after insert) {
if(trigger.isInsert){
AccountProcessor.countContacts(trigger.newMap.keySet());
}
}
Apex Class:
public class AccountProcessor {
public static integer count=0;
@future
public static void countContacts(set<Id> accountids){
List<Account> accountstobulkify=[SELECT Id, Name FROM Account WHERE Id IN:accountids];
Map<Id,Account> accountstoupdate=new Map<Id,Account>();
for(Account a:accountstobulkify){
for(Contact c:a.Contacts){
count=count++;// this line is not passed
}
a.Number_Of_Contacts__c=count;
}
}
}
Test class:
@isTest
private class AccountProcessorTest {
@isTest
static void CountNoOfContacts(){
Account testacc=new Account(Name='Numberofcontactstest',Industry='Finance');
insert testacc;
List<Contact> testlist=new List<Contact>();
for(integer i=0; i<10; i++){
Contact testcon=new Contact(LastName='AccountContact'+i, AccountId=testacc.id);
testlist.add(testcon);
}
insert testlist;
system.debug('size:'+testlist.size());
Map<Id,Account> testids=new Map<Id,Account>([SELECT Id, Name, Number_Of_Contacts__c FROM Account WHERE Name=:'Numberofcontactstest']);
Set<Id> accids=testids.keySet();
system.debug('Contcts:'+([SELECT Count() FROM Contact WHERE AccountId=:testacc.Id]));
test.startTest();
AccountProcessor.countContacts(accids);
test.stopTest();
decimal a=[SELECT ID, Name FROM Account WHERE Name=:'Numberofcontactstest'].Number_Of_Contacts__c;
system.assertEquals(10, a);
}
}
I have found your solution.
Please use below class & test class. It will give you 100% code coverage.
Class:
Test Class:
Implement it.
If you have any query please let me know.
Thanks,
Arvind Kumar
All Answers
I have found your solution.
Please use below class & test class. It will give you 100% code coverage.
Class:
Test Class:
Implement it.
If you have any query please let me know.
Thanks,
Arvind Kumar