Tag Archives: Future Method

Test Class For Future Method

Future Method:

public class AccountHelper {
	@future
	public static void UpdateAccounts(List<Id> accountIds){
		//Get those records based on the IDs
		List<Account> accList = [SELECT Name FROM Account WHERE Id IN : accountIds];
		//Process records
	}
}

Test Class:

@isTest
public class TestAccountHelper {
    
    private static User CreateUser(){
        String orgId = UserInfo.getOrganizationId();
        String standardUserProfileId = [SELECT Id From Profile WHERE Name = 'Standard User'].Id;
        User u = new User(
            FirstName = 'Test',
            LastName = 'User',
            Alias = 'TestUser', 
            profileId = standardUserProfileId ,
            Email = orgId + '@test.org',
            Username = orgId + '@test.org',
            EmailEncodingKey = 'ISO-8859-1',
            LanguageLocaleKey = 'en_US',
            LocaleSidKey = 'en_US',  
            TimeZoneSidKey = 'America/Los_Angeles'
        );
        insert u;
        return u;
    }
    
    private static Account CreateAccount(){
        Account acc = new Account( Name = 'Test Account', 
                                  Type = 'Technology Partner', 
                                  Industry = 'Technology',
                                  Phone = '9898989898'
                                 );
        insert acc;
        return acc;
    }
    
    private static testMethod void testUpdateAccounts(){
        
        User u = CreateUser();
        System.runAs(u) {
            Account acc = CreateAccount();
            List<Id> accountIds = new List<Id>();
            accountIds.add(acc.Id);
            
            Test.startTest();
            AccountHelper.UpdateAccounts(accountIds);
            Test.stopTest();
        }
        //Verify account is inserted
        List<Account> accList = [SELECT Id From Account WHERE Name = 'Test Account'];
        System.assertEquals(1, accList.size());
    }
}