i need to cover 75% but covering 22% only please help me to cover code in batch apex.
batch apex:
global class UpdateContactAcoount implements Database.Batchable<sObject> {
map<string,contact> conmap = new map<string,contact>();
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator(
'SELECT ID,name,Accountnumber, (SELECT ID, lastname, accountid FROM Contacts) FROM Account Where Accountnumber = \'123\''
);
}
global void execute(Database.BatchableContext bc, List<Account> scope){
account a = [select id,name,(select id,firstname,lastname,accountid from contacts) from account where name=:'bicsnn'];
map<id,contact> conacc = new map<id,contact>([select id,lastname,accountid from contact where id=: conmap.values()]);
map<string,contact> newmap = new map<string,contact>();
list<contact> setupdate = new list<contact>();
for(account acc : scope){
for(contact c : acc.contacts){
if(!conmap.containskey(c.lastname)){
conmap.put(c.lastname,c);
system.debug('oldcontact:'+conmap);
}
}
}
for(contact con :a.contacts){
newmap.put(con.lastname,con);
system.debug('newcontact:'+newmap);
}
for(string key : conmap.keyset()){
for(contact cc : conmap.values()){
if(!newmap.containskey(key)){
cc.accountid = a.id;
setupdate.add(cc);
system.debug('listcontact:'+setupdate);
}
}
}
update setupdate;
}
global void finish(Database.BatchableContext bc){
}
}
Test Class:
@istest
public class UpdateContactAcoount_Test {
static testmethod void testbatch(){
list<account> acc = new list<account>();
account a = new account();
a.name = 'test';
acc.add(a);
insert acc;
database.batchablecontext bc;
UpdateContactAcoount db = new UpdateContactAcoount();
db.start(bc);
db.execute(bc, acc);
db.finish(bc);
}
}