• XK Reyes
  • NEWBIE
  • 10 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
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
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);
        }
    }
}

 
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
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);
        }
    }
}