You need to sign in to do that
Don't have an account?

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);
}
}
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);
}
}
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
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);
}
}
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
Please mark it as best answer if it works for you.