+ Start a Discussion
Justin DurossJustin Duross 

Using Future Methods Trailhead Help

Hello, for the Salesforce Trailhead "Using Future Methods" I keep getting the error "The 'AccountProcessorTest' test class doesn't appear to be calling the 'AccountProcessor.countContacts' method between Test.startTest() and Test.stopTest()." I believe I have the right syntax, so I'm not sure what is wrong. My main class is fine. the problem only seems to be in the AccountProcessorTest.apxc

AccountProcessorTest.apxc
@isTest private class AccountProcessorTest {
    @isTest static void countContacts() {
        Test.setMock(AccountProcessorTest.class, new Account());
        Test.startTest();
        	AccountProcessor.countContacts();			
        Test.stopTest();
        
    } 	
}


Here is my main class in case anyone needs it:

AccountProcessor.apxc
global 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;
    }
}

Thank you in advance!
Justin Duross

 
Best Answer chosen by Justin Duross
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for same issue
1) https://developer.salesforce.com/forums/?id=906F0000000D8hwIAC

Please update your Test class like below:-
@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);
  }
  
}
Let us know if this will help you


 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please check below post for same issue
1) https://developer.salesforce.com/forums/?id=906F0000000D8hwIAC

Please update your Test class like below:-
@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);
  }
  
}
Let us know if this will help you


 
This was selected as the best answer
Justin DurossJustin Duross
Thank you! You test class did indeed work!
Javeed ShaikJaveed Shaik
Hi @Amit Chaudhary 8,

Please give me a clarification, Can we write code for this callenge like below (as Account 'a' is already available in the same method)
23        System.assertEquals ( a.Number_of_Contacts__c ,1);
Instead of like this (as you did)
22        Account ACC = [select Number_of_Contacts__c from Account where id = :a.id LIMIT 1];
23        System.assertEquals ( Integer.valueOf(ACC.Number_of_Contacts__c) ,1);
Thanks in advance,
Javeed
 
Amit Chaudhary 8Amit Chaudhary 8
you need to query "Number_of_Contacts__c" field to get the updated value.