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
mohan s 37mohan s 37 

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);
    }
}
 
Best Answer chosen by mohan s 37
Arvind KumarArvind Kumar
Hi Mohan,

I have found your solution.

Please use below class & test class. It will give you 100% code coverage.

Class:
public class AccountProcessor 
{
  @future
  public static void countContacts(Set<id> setId) 
  {
      List<Account> lstAccount = [select id,Number_of_Contacts__c , (select id from contacts ) from account where id in :setId ];
      for( Account acc : lstAccount )
      {
          List<Contact> lstCont = acc.contacts ;
          
          acc.Number_of_Contacts__c = lstCont.size();
      }
      update lstAccount;
  }
}

Test Class:
@IsTest
public class AccountProcessorTest {
    public static testmethod void TestAccountProcessorTest() 
    {
        Account a = new Account();
        a.Name = 'Test Account';
        Insert a;

        Contact cont = New Contact();
        cont.FirstName ='Bob';
        cont.LastName ='Masters';
        cont.AccountId = a.Id;
        Insert cont;
        
        set<Id> setAccId = new Set<ID>();
        setAccId.add(a.id);
 
        Test.startTest();
            AccountProcessor.countContacts(setAccId);
        Test.stopTest();
        
        Account ACC = [select Number_of_Contacts__c from Account where id = :a.id LIMIT 1];
        System.assertEquals ( Integer.valueOf(ACC.Number_of_Contacts__c) ,1);
  }
  
}


Implement it.

If you have any query please let me know.

Thanks,
Arvind Kumar

All Answers

Arvind KumarArvind Kumar
Hi Mohan,

I have found your solution.

Please use below class & test class. It will give you 100% code coverage.

Class:
public class AccountProcessor 
{
  @future
  public static void countContacts(Set<id> setId) 
  {
      List<Account> lstAccount = [select id,Number_of_Contacts__c , (select id from contacts ) from account where id in :setId ];
      for( Account acc : lstAccount )
      {
          List<Contact> lstCont = acc.contacts ;
          
          acc.Number_of_Contacts__c = lstCont.size();
      }
      update lstAccount;
  }
}

Test Class:
@IsTest
public class AccountProcessorTest {
    public static testmethod void TestAccountProcessorTest() 
    {
        Account a = new Account();
        a.Name = 'Test Account';
        Insert a;

        Contact cont = New Contact();
        cont.FirstName ='Bob';
        cont.LastName ='Masters';
        cont.AccountId = a.Id;
        Insert cont;
        
        set<Id> setAccId = new Set<ID>();
        setAccId.add(a.id);
 
        Test.startTest();
            AccountProcessor.countContacts(setAccId);
        Test.stopTest();
        
        Account ACC = [select Number_of_Contacts__c from Account where id = :a.id LIMIT 1];
        System.assertEquals ( Integer.valueOf(ACC.Number_of_Contacts__c) ,1);
  }
  
}


Implement it.

If you have any query please let me know.

Thanks,
Arvind Kumar

This was selected as the best answer
mohan s 37mohan s 37
Thank you, Arvind, My issue is resolved now. I am so grateful to you for spending your valuable efforts to solve this issue. I have learned something from your post.The way you are writing the code is very interesting and it reduces the number of lines.