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
Aryan JhaAryan Jha 

why the test is getting 0%

@istest
public class AccountProcessorTest {
@istest
    static void CountContactTest()
    {
        List<Account>accounts=new List<Account>();
        for(Integer i=0;i<300;i++)
        {
            accounts.add(new Account(Name='test account'+i));
        }
        insert accounts;
        List<Contact>contacts=new List<Contact>();
        List<Id>accountId=new List<Id>();
        for(Account a:accounts)
        {
            contacts.add(new Contact(Firstname=a.name,Lastname='testname',AccountId=a.id));
            accountId.add(a.id);    
        }
        insert contacts;
        Test.startTest();
        AccountProcessor.countContacts(accountId);
        Test.stopTest();
        List<Account>accs=[SELECT Id,Number_Of_Contacts__c FROM Account];
        for(Account acc:accs)
        {
            system.assertEquals(1,acc.Number_Of_Contacts__c);
        }
    }


}
ANUTEJANUTEJ (Salesforce Developers) 
Hi Aryan,

Can you mention the snippet you have implemented that you are testing using this class?

Thanks
Aryan JhaAryan Jha
public class AccountProcessor { @future public static void countContacts(ListaccountIds) { Listaccounts=[SELECT Id,(SELECT Id FROM Contacts) FROM Account WHERE Id IN:accountIds]; for(Account acc:accounts) { acc.Number_Of_Contacts__c=acc.Contacts.size(); } update accounts; } }
Suraj Tripathi 47Suraj Tripathi 47
Hi Aryan, 
Your code seems to be correct.
But the AccountPorcess apex class you have shown below in that class the parameter should be List<Id> accountsIds.
You should change and rerun the test class.
Hope it will pass and coverage will be above 90%.
Please mark it as the best answer if it helps you to fix the issue.
Thank and Regards,
Suraj Tripathi