Skip to main content
Hello all,

I am new to Apex and was working on a challenge. I seem to encountring an error.

"The 'AccountProcessor' class did not achieve 100% code coverage via your test methods. Make sure that you chose 'Run All' tests in the Developer Console at least once before attempting to verify this challenge."

The task is as mentioned below and my codes are also alongside. Could someone please let me know what wrong am I doing and how to correct it?

Thanks in advance!

 

public class AccountProcessor {

@future

public static void countContacts(List<Id> accountIds) {

List<Account> accounts = [Select Id, Name from Account Where Id IN :accountIds];

// process account records to do awesome stuff

List<Contact> con = [select id from contact where AccountId in :accounts];

for(Account a : accounts){

a.Number_Of_Contacts__c=con.size();

}

}

}

 

@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;

List<Id> setAccId = new List<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);

}

}

Name: AccountProcessor

Method name: countContacts

The method must accept a List of Account IDs

The method must use the @future annotation

The method counts the number of Contact records associated to each Account ID passed to the method and updates the 'Number_Of_Contacts__c' field with this value

Create an Apex test class:

Name: AccountProcessorTest

The unit tests must cover all lines of code included in the AccountProcessor class, resulting in 100% code coverage.

Before verifying this challenge, run your test class at least once using the Developer Console Run All feature
7 answers
  1. Jun 23, 2023, 4:43 PM
    Just discovered that there was an exception which said that the insert was not successful and then I tried to do a manual insertion from the UI and then realized there was a validation rule which was preventing creation of a new account. After deactivating that it was successful.
0/9000