function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Guru 91Guru 91 

Test class for Account class

Hi Everyone,
can you please help me with test class


public without sharing class AccountHelperUtility {
  
   //update account area fields 
    public static void insertUpdateAccountAreaFields(List<Account> AccountList, Map<ID,sObject> AccountMapOld) { 
    for(Account account:AccountList) {
        Account accountOld = AccountMapOld == null ? null : (Account)AccountMapOld.get(account.id);
      populateValuesBeforeInsertUpdate(account, accountOld, 'Planned_Retention_Tsubo__c', 'Planned_Retention_SF__c', 'Planned_Retention_SM__c');
      populateValuesBeforeInsertUpdate(account, accountOld, 'Customer_Total_Footprint_Wallet_Tsubo__c', 'Customer_Total_Footprint_Wallet_SF__c', 'Customer_Total_Footprint_Wallet_SM__c');
    }   
    }
    
    static void populateValuesBeforeInsertUpdate(Account account, Account accountOld, String Tsubo, String SF, String SM) {
        Boolean isUpdate = accountOld == null ? false : true;
      if(account.get(SF) != null && String.valueOf(account.get(SF))!= '' && (!isUpdate || Integer.valueOf(account.get(SF)) != Integer.valueOf(accountOld.get(SF))) ){
        account.put(Tsubo , UOMCalculation.getTsuboFromSf((decimal)account.get(SF)));
        account.put(SM , UOMCalculation.getM2FromSf((decimal)account.get(SF)));
      }else if(account.get(SM) != null && String.valueOf(account.get(SM))!= '' && (!isUpdate || Integer.valueOf(account.get(SM)) != Integer.valueOf(accountOld.get(SM))) ){
          Decimal sqFt = UOMCalculation.getSfFromM2((decimal)account.get(SM));
        account.put(Tsubo , UOMCalculation.getTsuboFromSf(sqFt));
        account.put(SF , sqFt);
      }else if(account.get(Tsubo) != null && String.valueOf(account.get(Tsubo))!= '' && (!isUpdate || Integer.valueOf(account.get(Tsubo)) != Integer.valueOf(accountOld.get(Tsubo))) ) {
          Decimal sqFt = UOMCalculation.getSfFromTsubo((decimal)account.get(Tsubo));
      account.put(SM , UOMCalculation.getM2FromSf(sqFt));
      account.put(SF , sqFt);
      }
    }
    
     //Added for T-131999
    public static void removeTeamMembers(Map<Id,Account> accountNewMap, Map<Id,Account> accountOldMap){
      map<Id,Id> accIdToGCSOfficerMap = new map<Id,Id>();
      list<AccountTeamMember> teamMembersToBeDeleted = new list<AccountTeamMember>();
      for(Account acc : accountNewMap.values()){
        if((acc.GCS_Account_Officer__c != null && 
          acc.GCS_Account_Officer__c != accountOldMap.get(acc.Id).GCS_Account_Officer__c)){
            accIdToGCSOfficerMap.put(acc.Id,acc.GCS_Account_Officer__c);
        }
      }
      if(accIdToGCSOfficerMap.size() > 0 ){
        for(AccountTeamMember accTeamMember : [Select Id,UserId,AccountId from AccountTeamMember where AccountId IN : 
          accIdToGCSOfficerMap.keySet()]){
            if(accTeamMember.AccountId != null && accIdToGCSOfficerMap.containsKey(accTeamMember.AccountId) &&
              accTeamMember.UserId != null && accIdToGCSOfficerMap.get(accTeamMember.AccountId) == accTeamMember.UserId){
                teamMembersToBeDeleted.add(accTeamMember);
              }
        }
      }
      if(teamMembersToBeDeleted.size() > 0){
        delete teamMembersToBeDeleted;
      }
    }
}
Best Answer chosen by Guru 91
Steven NsubugaSteven Nsubuga
@isTest
private class AccountHelperUtilityTest {

    @isTest static void test() {
        
        Profile pf= [Select Id from profile where Name='Standard User']; 
        String orgId=UserInfo.getOrganizationId(); 
        
        User GCSUser=new User(firstname = 'GCS', 
                         lastName = 'User', 
                         email =  'gcs@test' + orgId + '.org', 
                         Username = 'gcs@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = 'ABC', 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 

        insert GCSUser;
        
        User GCSUser2=new User(firstname = 'GCGS', 
                         lastName = 'Test', 
                         email =  'gcgs@test' + orgId + '.org', 
                         Username = 'gcgs@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = 'ABC', 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 

        insert GCSUser2;
        
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email =  'uniqueName@test' + orgId + '.org', 
                         Username = 'uniqueName@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = 'ABC', 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 

        insert uu;
        system.runAs(uu){    
            List<Account> accts = new List<Account>();
            Map<ID, sObject> accountMapOld = new Map<ID,sObject>();
            Map<ID, Account> accountMapOld2 = new Map<ID,Account> ();
            Map<ID, Account> accountMapNew = new Map<ID,Account> ();
            for (Integer i = 1; i < 6; i++) {
                Account testAccount = new Account(Name='Test Account'+i, GCS_Account_Officer__c = GCSUser.Id, Planned_Retention_Tsubo__c = 1, 
                Planned_Retention_SF__c = 2, Planned_Retention_SM__c = 3, Customer_Total_Footprint_Wallet_Tsubo__c = i, Customer_Total_Footprint_Wallet_SF__c = i + 2,
                Customer_Total_Footprint_Wallet_SM__c = i + 3);
                accts.add(testAccount);
            }
            insert accts;
            
            list<AccountTeamMember> teamMembers = new list<AccountTeamMember>();
            
            for (Account testAccount : accts) {
                accountMapOld.put(testAccount.id, testAccount);
                accountMapOld2.put(testAccount.id, testAccount);
                accountMapNew.put(testAccount.id, testAccount);
                teamMembers.add(new AccountTeamMember (UserId = uu.Id, AccountId = testAccount.Id));
            }
            insert teamMembers;
            
            for (Account testAccount : accountMapNew.values()) {
                testAccount.GCS_Account_Officer__c  = GCSUser2.Id;
            }
            update accountMapNew.values();
            
            AccountHelperUtility.insertUpdateAccountAreaFields(accts, accountMapOld);
            AccountHelperUtility.removeTeamMembers(accountMapNew, accountMapOld2);
            list<AccountTeamMember> teamMembers2 = [Select Id,UserId,AccountId from AccountTeamMember];
            System.assert(teamMembers2.size()==0);
        }
    }
    
}

All Answers

Steven NsubugaSteven Nsubuga
Try this
@isTest
private class AccountHelperUtilityTest {

	@isTest static void test() {
		
		Profile pf= [Select Id from profile where Name='Standard User']; 
		String orgId=UserInfo.getOrganizationId(); 
		User uu=new User(firstname = 'ABC', 
						 lastName = 'XYZ', 
						 email =  'uniqueName@test' + orgId + '.org', 
						 Username = 'uniqueName@test' + orgId + '.org', 
						 EmailEncodingKey = 'ISO-8859-1', 
						 Alias = 'ABC', 
						 TimeZoneSidKey = 'America/Los_Angeles', 
						 LocaleSidKey = 'en_US', 
						 LanguageLocaleKey = 'en_US', 
						 ProfileId = pf.Id
						); 

		insert uu;
		system.runAs(uu){	
			List<Account> accts = new List<Account>();
			Map<ID, sObject> accountMapOld = new Map<ID,sObject>();
			Map<ID, Account> accountMapOld2 = new Map<ID,Account> ();
			Map<ID, Account> accountMapNew = new Map<ID,Account> ();
			for (Integer i = 1; i < 6; i++) {
				Account testAccount = new Account(Name='Test Account'+i, GCS_Account_Officer__c = 'Test', Planned_Retention_Tsubo__c = 1, 
				Planned_Retention_SF__c = 2, Planned_Retention_SM__c = 3, Customer_Total_Footprint_Wallet_Tsubo__c = i, Customer_Total_Footprint_Wallet_SF__c = i + 2,
				Customer_Total_Footprint_Wallet_SM__c = i + 3);
				accts.add(testAccount);
			}
			insert accts;
			
			list<AccountTeamMember> teamMembers = new list<AccountTeamMember>();
			
			for (Account testAccount : accts) {
				accountMapOld.put(testAccount.id, testAccount);
				accountMapOld2.put(testAccount.id, testAccount);
				accountMapNew.put(testAccount.id, testAccount);
				teamMembers.add(new AccountTeamMember (UserId = uu.Id, AccountId = testAccount.Id));
			}
			insert teamMembers;
			
			for (Account testAccount : accountMapNew.values()) {
				testAccount.GCS_Account_Officer__c  = 'test2';
			}
			update accountMapNew.values();
			
			AccountHelperUtility.insertUpdateAccountAreaFields(accts, accountMapOld);
			AccountHelperUtility.removeTeamMembers(accountMapNew, accountMapOld);
			list<AccountTeamMember> teamMembers2 = [Select Id,UserId,AccountId from AccountTeamMember];
			System.assert(teamMembers2.size()==0);
		}
	}
	
}

 
Guru 91Guru 91
Thank so much Steven Nsubuga,
I am getting this error while saving class

Error: Compile Error: Method does not exist or incorrect signature: void removeTeamMembers(Map<Id,Account>, Map<Id,SObject>) from the type AccountHelperUtility at line 50 column 34
Puspack TravelPuspack Travel
thanks. I am also facing the same problem. finally i get resolve the problem.
thank u so much
regarding 
Pusapck (https://www.puspack.com/)
Steven NsubugaSteven Nsubuga
@isTest
private class AccountHelperUtilityTest {

	@isTest static void test() {
		
		Profile pf= [Select Id from profile where Name='Standard User']; 
		String orgId=UserInfo.getOrganizationId(); 
		User uu=new User(firstname = 'ABC', 
						 lastName = 'XYZ', 
						 email =  'uniqueName@test' + orgId + '.org', 
						 Username = 'uniqueName@test' + orgId + '.org', 
						 EmailEncodingKey = 'ISO-8859-1', 
						 Alias = 'ABC', 
						 TimeZoneSidKey = 'America/Los_Angeles', 
						 LocaleSidKey = 'en_US', 
						 LanguageLocaleKey = 'en_US', 
						 ProfileId = pf.Id
						); 

		insert uu;
		system.runAs(uu){	
			List<Account> accts = new List<Account>();
			Map<ID, sObject> accountMapOld = new Map<ID,sObject>();
			Map<ID, Account> accountMapOld2 = new Map<ID,Account> ();
			Map<ID, Account> accountMapNew = new Map<ID,Account> ();
			for (Integer i = 1; i < 6; i++) {
				Account testAccount = new Account(Name='Test Account'+i, GCS_Account_Officer__c = 'Test', Planned_Retention_Tsubo__c = 1, 
				Planned_Retention_SF__c = 2, Planned_Retention_SM__c = 3, Customer_Total_Footprint_Wallet_Tsubo__c = i, Customer_Total_Footprint_Wallet_SF__c = i + 2,
				Customer_Total_Footprint_Wallet_SM__c = i + 3);
				accts.add(testAccount);
			}
			insert accts;
			
			list<AccountTeamMember> teamMembers = new list<AccountTeamMember>();
			
			for (Account testAccount : accts) {
				accountMapOld.put(testAccount.id, testAccount);
				accountMapOld2.put(testAccount.id, testAccount);
				accountMapNew.put(testAccount.id, testAccount);
				teamMembers.add(new AccountTeamMember (UserId = uu.Id, AccountId = testAccount.Id));
			}
			insert teamMembers;
			
			for (Account testAccount : accountMapNew.values()) {
				testAccount.GCS_Account_Officer__c  = 'test2';
			}
			update accountMapNew.values();
			
			AccountHelperUtility.insertUpdateAccountAreaFields(accts, accountMapOld);
			AccountHelperUtility.removeTeamMembers(accountMapNew, accountMapOld2);
			list<AccountTeamMember> teamMembers2 = [Select Id,UserId,AccountId from AccountTeamMember];
			System.assert(teamMembers2.size()==0);
		}
	}
	
}

 
Guru 91Guru 91
Hi Steven,
Class saved but Test failed


Error
System.StringException: Invalid id: Test
Stack trace
Class.AccountHelperUtilityTest.test: line 27, column 1
Guru 91Guru 91
Hi Steven,
GCS_Account_Officer__c is a lookup with user object
Steven NsubugaSteven Nsubuga
@isTest
private class AccountHelperUtilityTest {

    @isTest static void test() {
        
        Profile pf= [Select Id from profile where Name='Standard User']; 
        String orgId=UserInfo.getOrganizationId(); 
        
        User GCSUser=new User(firstname = 'GCS', 
                         lastName = 'User', 
                         email =  'gcs@test' + orgId + '.org', 
                         Username = 'gcs@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = 'ABC', 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 

        insert GCSUser;
        
        User GCSUser2=new User(firstname = 'GCGS', 
                         lastName = 'Test', 
                         email =  'gcgs@test' + orgId + '.org', 
                         Username = 'gcgs@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = 'ABC', 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 

        insert GCSUser2;
        
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email =  'uniqueName@test' + orgId + '.org', 
                         Username = 'uniqueName@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = 'ABC', 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 

        insert uu;
        system.runAs(uu){    
            List<Account> accts = new List<Account>();
            Map<ID, sObject> accountMapOld = new Map<ID,sObject>();
            Map<ID, Account> accountMapOld2 = new Map<ID,Account> ();
            Map<ID, Account> accountMapNew = new Map<ID,Account> ();
            for (Integer i = 1; i < 6; i++) {
                Account testAccount = new Account(Name='Test Account'+i, GCS_Account_Officer__c = GCSUser.Id, Planned_Retention_Tsubo__c = 1, 
                Planned_Retention_SF__c = 2, Planned_Retention_SM__c = 3, Customer_Total_Footprint_Wallet_Tsubo__c = i, Customer_Total_Footprint_Wallet_SF__c = i + 2,
                Customer_Total_Footprint_Wallet_SM__c = i + 3);
                accts.add(testAccount);
            }
            insert accts;
            
            list<AccountTeamMember> teamMembers = new list<AccountTeamMember>();
            
            for (Account testAccount : accts) {
                accountMapOld.put(testAccount.id, testAccount);
                accountMapOld2.put(testAccount.id, testAccount);
                accountMapNew.put(testAccount.id, testAccount);
                teamMembers.add(new AccountTeamMember (UserId = uu.Id, AccountId = testAccount.Id));
            }
            insert teamMembers;
            
            for (Account testAccount : accountMapNew.values()) {
                testAccount.GCS_Account_Officer__c  = GCSUser2.Id;
            }
            update accountMapNew.values();
            
            AccountHelperUtility.insertUpdateAccountAreaFields(accts, accountMapOld);
            AccountHelperUtility.removeTeamMembers(accountMapNew, accountMapOld2);
            list<AccountTeamMember> teamMembers2 = [Select Id,UserId,AccountId from AccountTeamMember];
            System.assert(teamMembers2.size()==0);
        }
    }
    
}
This was selected as the best answer