You need to sign in to do that
Don't have an account?
XK Reyes
Trying to get 100% Test Coverage
I'm trying to achieve a 100% coverage but I'm only getting 71%. Lines 7-8 on the AccountProcessor class are uncovered by my test class. Any ideas?.
AccountProcessor Class
AccountProcessor Class
public class AccountProcessor { @future public static void countContacts(List<Id> accountIds) { List<Account> acctsToUpdate = new List<Account>(); for (Account a : [SELECT Id, (SELECT Id FROM Contacts) FROM Account WHERE Id IN: accountIds]){ a.Number_of_Contacts__c = a.Contacts.size(); acctsToUpdate.add(a); } update acctsToUpdate; } }AccountProcessor Test Class
@isTest public class AccountProcessorTest { @isTest public static void testCountOfContacts() { List<Account> newAccts = new List<Account>(); List<Contact> newConts = new List<Contact>(); for(Integer x=0;x>5;x++) { newAccts.add(new Account(Name='New Account '+x)); } insert newAccts; for(Account a:newAccts) { for(Integer y=0;y>2;y++){ newConts.add(new Contact(LastName=a.Name+y, AccountId=a.Id)); } } insert newConts; List<Id> accountIds = new List<Id>(new Map<Id,Account>(newAccts).keySet()); Test.startTest(); AccountProcessor.countContacts(accountIds); Test.stopTest(); for(Account a: [SELECT Id, Number_of_Contacts__c FROM Account WHERE Id IN: accountIds]){ System.assertEquals(2, a.Number_of_Contacts__c); } } }
Please change your test class to the below code: Explanation: I changed the for loop condition to x<5 where its creating accounts in your code. The for loop in previous test class will never execute as the x=0 and it is not greater than 5. It comes out of the loop and accounts are not created same with contacts for loop.
Your code should get 100% coverage now. I tested it
Hope this helps! Please mark as best if it answers your problem.
Thanks
All Answers
Please change your test class to the below code: Explanation: I changed the for loop condition to x<5 where its creating accounts in your code. The for loop in previous test class will never execute as the x=0 and it is not greater than 5. It comes out of the loop and accounts are not created same with contacts for loop.
Your code should get 100% coverage now. I tested it
Hope this helps! Please mark as best if it answers your problem.
Thanks