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
PappuPappu 

Apex test class is not working

Hi Guys,

I've written and executing the test class for below code and not getting a enough code coverage. Any idea what I am miising in the code?

I could see, the log is returning null values to the line (List<AccountTeamMember> accountTeamList = [select userId,AccountId from AccountTeamMember where userId IN :userIds ];) though I've enough records to satisfy the select querry.
Class

global class Accountteamroleupdate {
    @future
    public static void role(Set<Id> userIds) {
        system.debug('User Id is ' + userIds);
        List<AccountTeamMember> accountTeamList = [select userId,AccountId from AccountTeamMember where userId IN :userIds ];
        map<Id, user> ObjMap = new map<Id, user>([select User_ID_18__c,Taylor_Business_Unit__c, Function__c  from user where Id IN :userIds]);
        System.debug('AccountTeamList is ' + accountTeamList);
        List<AccountTeamMember> updatedaccountlist = new List<AccountTeamMember>();
        for (AccountTeamMember am : accountTeamList )
        {
            if(ObjMap.containsKey(am.userId))
            {  
                User obj = ObjMap.get(am.userId);
                AccountTeamMember teamrole =new AccountTeamMember();
                teamrole.userId = obj.User_ID_18__c;
                teamrole.AccountId= am.AccountId;
                if (obj.Taylor_Business_Unit__c != null && obj.Function__c != null)
                {
                    teamrole.TeamMemberRole = obj.Taylor_Business_Unit__c +' '+ obj.Function__c;
                }
                else 
                {
                    teamrole.TeamMemberRole = 'Unspecified';
                }
                updatedaccountlist.add(teamrole);
            }
            
        }
        insert updatedaccountlist;
    }
}

Test Class:

@isTest
public class Teamroleupdatetestcls {
    static testMethod void testAccountTrigger(){
        user ul = [select Id from user where User_ID_18__c='005Z0000003r5wMIAQ'];
        system.debug('UserId is ' + ul.Id);
        set<Id> setAccId = new Set<ID>();
        setAccId.add(ul.Id);
        
        Accountteamroleupdate.role(setAccId);
          
    }
}
Best Answer chosen by Pappu
devedeve
Hi Pappu,

Please try this code:-

@isTest
public class Teamroleupdatetestcls {

    static testMethod void testAccountTrigger(){
        user ul = [select Id from user where User_ID_18__c='005Z0000003r5wMIAQ'];
        system.debug('UserId is ' + ul.Id);
        AccountTeamMember acc = new AccountTeamMember(Name='testAccount');
        acc.userId = ul.Id;
​        insert acc;
        set<Id> setAccId = new Set<ID>();
        setAccId.add(ul.Id);
        
        Accountteamroleupdate.role(setAccId);
          
    }
}

All Answers

devedeve
Hi Pappu,

Please try this code:-

@isTest
public class Teamroleupdatetestcls {

    static testMethod void testAccountTrigger(){
        user ul = [select Id from user where User_ID_18__c='005Z0000003r5wMIAQ'];
        system.debug('UserId is ' + ul.Id);
        AccountTeamMember acc = new AccountTeamMember(Name='testAccount');
        acc.userId = ul.Id;
​        insert acc;
        set<Id> setAccId = new Set<ID>();
        setAccId.add(ul.Id);
        
        Accountteamroleupdate.role(setAccId);
          
    }
}
This was selected as the best answer
PappuPappu
Hi deve,

Your code did the magic!! I am able to get the bettter code coverage. Thanks a lot :) :).

Can you point out what exacly I missed out in the test class?

Regards,
Pappu
devedeve
Yes you have not insert AccountTeamMember having userId which you have fetched in ul variable so all the calculation based on AccountTeamMember was not happening.
Please mark it as best answer if it works for you.