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

Test class code coverage in batch apex
Hi,
As I am unable to complete the code coverage in batch apex.
global class countContactRecordsBatch implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext bc) {
string query='select id,Census_Date__c from hed__Term__c where Census_Date__c=LAST_N_DAYS:1';
system.debug('query' +query) ;
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc, List<hed__Term__c> scope){
list<hed__Term__c> termList=[select id from hed__Term__c where Census_Date__c=LAST_N_DAYS:1];
list<hed__Course_Offering__c> moduleOfferingList=[select hed__Term__r.id,(select hed__Contact__r.id,hed__Program_Enrollment__r.Program__r.id from hed__Course_Enrollment__r) from hed__Course_Offering__c where hed__Term__r.id IN:termList];
Map<string,set<string>> termProgramContacts=new Map<string,set<string>>();
for(hed__Course_Offering__c co:moduleOfferingList) {
system.debug('Term Id-'+co.hed__Term__r.id);
for(hed__Course_Enrollment__c ce:co.hed__Course_Enrollment__r){
string mapKey = co.hed__Term__r.id+'-'+ce.hed__Program_Enrollment__r.Program__r.id;
if(ce.hed__Program_Enrollment__r.Program__r.id!=null){
if(termProgramContacts.containsKey(mapKey)){
set<string> existingValue = termProgramContacts.get(mapKey);
existingValue.add(ce.hed__Contact__r.id);
termProgramContacts.put(mapKey, existingValue);
}
else{
set<string> newValue = new set<string>();
newValue.add(ce.hed__Contact__r.id);
termProgramContacts.put(mapKey,newValue);
}
}
}
}
system.debug('Program Contact- '+termProgramContacts);
list<hed__Term__c> termProgramList=new list<hed__Term__c>();
list <Program_Session__c> programSessionList = new list <Program_Session__c>();
termProgramList=[select id,(select id,Program__r.id from Program_Sessions__r) from hed__Term__c where id IN:termList];
Map<string,integer> updateCount = new Map<string,integer>();
for(hed__Term__c termProgram:termProgramList){
for(Program_Session__c ps:termProgram.Program_Sessions__r){
string tpKey = termProgram.id+'-'+ps.Program__r.id;
if(termProgramContacts.containsKey(tpKey)){
integer contactCount = termProgramContacts.get(tpKey).size();
integer existingCount = 0;
if(updateCount.containsKey(ps.id)){
existingCount = updateCount.get(ps.id);
}
updateCount.put(ps.id,contactCount+existingCount);//progran term id and count
system.debug('updateCount'+updateCount.values().size());
}
ps.Total_enrolments_actual__c=updateCount.get(ps.Id);
programSessionList.add(ps);
system.debug('Total_enrolments_actual__c' +ps.Total_enrolments_actual__c);
system.debug('termProgramList ' +termProgramList);
}
}
if(programSessionList!=null && !programSessionList.isEmpty()){
update programSessionList;
}
}
global void finish(Database.BatchableContext BC){
}
//code end
}
===
Test class
=====
@isTest
public class countContactRecordsBatchTest {
static testMethod void contactRecords(){
list<Program_Session__c> psList=new list<Program_Session__c>();
list<Account> acc=new list<Account>();
list<hed__Term__c> termList=new list<hed__Term__c>();//term
list<Program__c> programList=new list<Program__c>();
list<hed__Course_Offering__c> mcList=new list<hed__Course_Offering__c>();
list<hed__Course__c> courseList=new list<hed__Course__c>();
list<hed__Program_Enrollment__c> prEnrollmentList=new list<hed__Program_Enrollment__c>();
list<Contact> consList=new list<contact>();
Account a=new account();
a.Name='Test Account';
acc.add(a);
insert acc;
hed__Term__c te=new hed__Term__c();
te.Name='test term';
te.hed__Account__c=acc[0].id;
te.Census_Date__c=date.today();
termList.add(te);
insert termList;//Term
Program__c pc=new Program__c();
pc.Name='test program ';
programList.add(pc);//Program ..
//insert module offering
hed__Course_Offering__c mc=new hed__Course_Offering__c();
hed__Course__c cou=new hed__Course__c();
cou.name='test course';
cou.hed__Account__c=acc[0].Id;
courseList.add(cou);
insert courseList;
mc.Name='Test Module Offering';
mc.hed__Course__c=courseList[0].id;
mc.hed__Term__c=termList[0].id;
mcList.add(mc);
//Insert contact
contact con=new contact();
con.lastName='test contact Name';
consList.add(con);
//program Enrollment
hed__Program_Enrollment__c pe=new hed__Program_Enrollment__c();
pe.Program__c=programList[0].id;
pe.hed__Contact__c=consList[0].id;
prEnrollmentList.add(pe);
Program_Session__c ps=new Program_Session__c();
ps.Term__c=termList[0].id;
// ps.Program__c=programList[0].id;
ps.Program__c=programList[0].id;
//ps.Total_enrolments_actual__c=1;
psList.add(ps);
database.insert (psList,false);
list<Program_Session__c> prs=[select id ,Term__r.id,Program__r.id,Total_enrolments_actual__c from Program_Session__c];
for(Program_Session__c pccc:prs){
pccc.Total_enrolments_actual__c=10;
}
test.startTest();
database.SaveResult []sr=database.update(prs,false);
countContactRecordsBatch obj=new countContactRecordsBatch();
database.executeBatch(obj);
test.stopTest();
}
}
As I am getting 44% code coverage on it.
Any suggestions.
Thanks
As I am unable to complete the code coverage in batch apex.
global class countContactRecordsBatch implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext bc) {
string query='select id,Census_Date__c from hed__Term__c where Census_Date__c=LAST_N_DAYS:1';
system.debug('query' +query) ;
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc, List<hed__Term__c> scope){
list<hed__Term__c> termList=[select id from hed__Term__c where Census_Date__c=LAST_N_DAYS:1];
list<hed__Course_Offering__c> moduleOfferingList=[select hed__Term__r.id,(select hed__Contact__r.id,hed__Program_Enrollment__r.Program__r.id from hed__Course_Enrollment__r) from hed__Course_Offering__c where hed__Term__r.id IN:termList];
Map<string,set<string>> termProgramContacts=new Map<string,set<string>>();
for(hed__Course_Offering__c co:moduleOfferingList) {
system.debug('Term Id-'+co.hed__Term__r.id);
for(hed__Course_Enrollment__c ce:co.hed__Course_Enrollment__r){
string mapKey = co.hed__Term__r.id+'-'+ce.hed__Program_Enrollment__r.Program__r.id;
if(ce.hed__Program_Enrollment__r.Program__r.id!=null){
if(termProgramContacts.containsKey(mapKey)){
set<string> existingValue = termProgramContacts.get(mapKey);
existingValue.add(ce.hed__Contact__r.id);
termProgramContacts.put(mapKey, existingValue);
}
else{
set<string> newValue = new set<string>();
newValue.add(ce.hed__Contact__r.id);
termProgramContacts.put(mapKey,newValue);
}
}
}
}
system.debug('Program Contact- '+termProgramContacts);
list<hed__Term__c> termProgramList=new list<hed__Term__c>();
list <Program_Session__c> programSessionList = new list <Program_Session__c>();
termProgramList=[select id,(select id,Program__r.id from Program_Sessions__r) from hed__Term__c where id IN:termList];
Map<string,integer> updateCount = new Map<string,integer>();
for(hed__Term__c termProgram:termProgramList){
for(Program_Session__c ps:termProgram.Program_Sessions__r){
string tpKey = termProgram.id+'-'+ps.Program__r.id;
if(termProgramContacts.containsKey(tpKey)){
integer contactCount = termProgramContacts.get(tpKey).size();
integer existingCount = 0;
if(updateCount.containsKey(ps.id)){
existingCount = updateCount.get(ps.id);
}
updateCount.put(ps.id,contactCount+existingCount);//progran term id and count
system.debug('updateCount'+updateCount.values().size());
}
ps.Total_enrolments_actual__c=updateCount.get(ps.Id);
programSessionList.add(ps);
system.debug('Total_enrolments_actual__c' +ps.Total_enrolments_actual__c);
system.debug('termProgramList ' +termProgramList);
}
}
if(programSessionList!=null && !programSessionList.isEmpty()){
update programSessionList;
}
}
global void finish(Database.BatchableContext BC){
}
//code end
}
===
Test class
=====
@isTest
public class countContactRecordsBatchTest {
static testMethod void contactRecords(){
list<Program_Session__c> psList=new list<Program_Session__c>();
list<Account> acc=new list<Account>();
list<hed__Term__c> termList=new list<hed__Term__c>();//term
list<Program__c> programList=new list<Program__c>();
list<hed__Course_Offering__c> mcList=new list<hed__Course_Offering__c>();
list<hed__Course__c> courseList=new list<hed__Course__c>();
list<hed__Program_Enrollment__c> prEnrollmentList=new list<hed__Program_Enrollment__c>();
list<Contact> consList=new list<contact>();
Account a=new account();
a.Name='Test Account';
acc.add(a);
insert acc;
hed__Term__c te=new hed__Term__c();
te.Name='test term';
te.hed__Account__c=acc[0].id;
te.Census_Date__c=date.today();
termList.add(te);
insert termList;//Term
Program__c pc=new Program__c();
pc.Name='test program ';
programList.add(pc);//Program ..
//insert module offering
hed__Course_Offering__c mc=new hed__Course_Offering__c();
hed__Course__c cou=new hed__Course__c();
cou.name='test course';
cou.hed__Account__c=acc[0].Id;
courseList.add(cou);
insert courseList;
mc.Name='Test Module Offering';
mc.hed__Course__c=courseList[0].id;
mc.hed__Term__c=termList[0].id;
mcList.add(mc);
//Insert contact
contact con=new contact();
con.lastName='test contact Name';
consList.add(con);
//program Enrollment
hed__Program_Enrollment__c pe=new hed__Program_Enrollment__c();
pe.Program__c=programList[0].id;
pe.hed__Contact__c=consList[0].id;
prEnrollmentList.add(pe);
Program_Session__c ps=new Program_Session__c();
ps.Term__c=termList[0].id;
// ps.Program__c=programList[0].id;
ps.Program__c=programList[0].id;
//ps.Total_enrolments_actual__c=1;
psList.add(ps);
database.insert (psList,false);
list<Program_Session__c> prs=[select id ,Term__r.id,Program__r.id,Total_enrolments_actual__c from Program_Session__c];
for(Program_Session__c pccc:prs){
pccc.Total_enrolments_actual__c=10;
}
test.startTest();
database.SaveResult []sr=database.update(prs,false);
countContactRecordsBatch obj=new countContactRecordsBatch();
database.executeBatch(obj);
test.stopTest();
}
}
As I am getting 44% code coverage on it.
Any suggestions.
Thanks
Its not easy to figure this out without having the same custom objects used in the code, in my developer account as well.
If you could, export the object defintion and attach the files here so the same can be deployed.
Look at this post for more information: https://initaura.com/create-custom-objects-through-workbench/
Thanks,
Neha
Thanks
Ramakrishna
I can see in your test class that you have created program List but didn't insert it.
Can you add an insert statement and try running your test class to see if there is any improvement in code coverage?
Regards,
Puneet.