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

Test class not covering execute method in batch class
Hi All,
I have a requirement where account's user updated related contact's user should be populated with the same value. I have written a trigger which will get execute and passes the accountid to batchclass and updates the contact field.
Now, when I write the batchclass and call it from Test.starttest () and Test.stoptest(), it is not executing execute method of batch class.
Below is the sample code:
Trigger accountupdate on Account(before update){
Map<Id,Account> accmap = new Map<Id,Account>();
for(Account a : trigger.new) {
if(a.userid!=null && a.parentId!=null && trigger.oldmap.get(a.id).userid != trigger.newmap.get(a.id).userid)){
accmap.put(a.id,a);
}}
database.executeBatch(new AccountUpdateBatch(accmap));
------------------------------------------
Batch Class
global class AccountUpdateBatch implements Database.Batachable<sObject>{
Map<Id, Account> acctmap = new Map<Id,Account>();
global AccountUpdateBatch(Map<Id,Account> accmap){
acctmap = accmap;
}
global Database.getQuerylocator start(Database.BatachableContext bc){
return Database.getquerylocator([select id,name,userid,accountid from contact where accountid in:acctmap.keyset()];
global void execute(Database.BatchableContext bc, list<contact> scope){
List<Contact> updateconlist = new List<contact>();
for(contact con:scope){
if(con.AccountId!=null)
{
if(acctmap.get(con.AccountId).userid!=null)
con.userid = acctmap.get(con.accountid).userid;
updateconlist.add(con);
}
}
}
update updateconlist;
-----------------
Test class:
@isTest
public class TestAccountBatch{
@isTest
public static void testbatch(){
user uid = new user(lastname ='Test user');
insert u;
Account parentacc = new Account(name = 'Parent account',recordtypeid='xxxx');
insert parentacc;
List<Account> acclist = new List<Account>();
Map<ID, ACcount> acckeymap = new Map<ID,Account>();
for(Integer i=0;i<200;i++)
Account acc= new Account();
acc.name='Test'+i;
acc.recordtypeid ='XXXX';
acc.parentid=parentacc.id;
acclist.add(acc);
acckeymap.put(acc.id,acc);
}
insert acclist;
List<Account> updateuser = new List<Account>();
Map<Id,Account> updatedacctidmap = new Map<Id,Account>();
updateuser =[select id,userid from account where id in:acckeymap.keyset()];
for(Account acc:updateuser ){
acc.userid = u.id;
updatedacctidmap.put(acc.id,acc);
}
update updateuser;
Test.starttest();
AccountUpdateBatch obj = new AccountUpdateBatch(updatedacctidmap)
database.executeBatch(obj);
Test.stoptest();
}
}
The test class is only covering 31% code coverage. The code highlighted in bold is not covering. Appreciate it if anyone can help me to write the test class. Thanks!
I have a requirement where account's user updated related contact's user should be populated with the same value. I have written a trigger which will get execute and passes the accountid to batchclass and updates the contact field.
Now, when I write the batchclass and call it from Test.starttest () and Test.stoptest(), it is not executing execute method of batch class.
Below is the sample code:
Trigger accountupdate on Account(before update){
Map<Id,Account> accmap = new Map<Id,Account>();
for(Account a : trigger.new) {
if(a.userid!=null && a.parentId!=null && trigger.oldmap.get(a.id).userid != trigger.newmap.get(a.id).userid)){
accmap.put(a.id,a);
}}
database.executeBatch(new AccountUpdateBatch(accmap));
------------------------------------------
Batch Class
global class AccountUpdateBatch implements Database.Batachable<sObject>{
Map<Id, Account> acctmap = new Map<Id,Account>();
global AccountUpdateBatch(Map<Id,Account> accmap){
acctmap = accmap;
}
global Database.getQuerylocator start(Database.BatachableContext bc){
return Database.getquerylocator([select id,name,userid,accountid from contact where accountid in:acctmap.keyset()];
global void execute(Database.BatchableContext bc, list<contact> scope){
List<Contact> updateconlist = new List<contact>();
for(contact con:scope){
if(con.AccountId!=null)
{
if(acctmap.get(con.AccountId).userid!=null)
con.userid = acctmap.get(con.accountid).userid;
updateconlist.add(con);
}
}
}
update updateconlist;
-----------------
Test class:
@isTest
public class TestAccountBatch{
@isTest
public static void testbatch(){
user uid = new user(lastname ='Test user');
insert u;
Account parentacc = new Account(name = 'Parent account',recordtypeid='xxxx');
insert parentacc;
List<Account> acclist = new List<Account>();
Map<ID, ACcount> acckeymap = new Map<ID,Account>();
for(Integer i=0;i<200;i++)
Account acc= new Account();
acc.name='Test'+i;
acc.recordtypeid ='XXXX';
acc.parentid=parentacc.id;
acclist.add(acc);
acckeymap.put(acc.id,acc);
}
insert acclist;
List<Account> updateuser = new List<Account>();
Map<Id,Account> updatedacctidmap = new Map<Id,Account>();
updateuser =[select id,userid from account where id in:acckeymap.keyset()];
for(Account acc:updateuser ){
acc.userid = u.id;
updatedacctidmap.put(acc.id,acc);
}
update updateuser;
Test.starttest();
AccountUpdateBatch obj = new AccountUpdateBatch(updatedacctidmap)
database.executeBatch(obj);
Test.stoptest();
}
}
The test class is only covering 31% code coverage. The code highlighted in bold is not covering. Appreciate it if anyone can help me to write the test class. Thanks!
Please use below code
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh