You need to sign in to do that
Don't have an account?
Sai Theja
help me in test class to code coverage
I have a batch apex class and a test class. But im getting only 60% of code coverage please help me to get above 75%
Class:
global class FacultyearnGFCBatch implements Database.Batchable<sObject>
{
global Database.QueryLocator start(Database.BatchableContext bc)
{
integer year = system.today().year();
string str1 = (year-1)+'-'+(year);
string Campus1 = 'Boston';
string Campus2 = 'BRD';
string Campus3 = 'BOS';
string inputCampus1 = '\''+'%'+ Campus1+'%'+'\'';
string inputCampus2 = '\''+'%'+ Campus2+'%'+'\'' ;
string inputCampus3 = '\''+'%'+ Campus3+'%'+'\'' ;
String[] Str = new string[]{str1};
string result = '\''+String.join(Str,'\',\'')+'\'';
string Class1 = 'No Payment for this Class';
string Class2 = '\''+'%'+ Class1+'%'+'\'';
Id strFacultyTeachingAssignment = NEUSFIDHelper.retrieveRecordTypeId(APIPConstants.REC_TYPE_FACULTY_TEACHING_ASSIGNMENT, 'Contract');
string query1 = 'SELECT id,Account.Name,Cancelation_Fee_Scheduled__c,Canceled_Contract_Fee__c,Course_Section_ID__r.course_section_status__c,Compensation_Rate__r.name,Course_Section_ID__c,AccountId FROM Contract WHERE ';
query1=query1+'RecordType.Id IN('+'\''+strFacultyTeachingAssignment+'\''+')';
query1=query1+'AND Cancelation_Fee_Scheduled__c = True ';
query1=query1+'AND Course_Section_ID__r.course_section_status__c = \'Cancelled\' ';
query1=query1+'AND Canceled_Contract_Fee__c IN ( \'100% GFC Fee\',\'75% GFC Fee\') ';
query1=query1+'AND (NOT Compensation_Rate__r.name like '+Class2+')';
List<Contract> cancContractLst = Database.query(query1);
string query2 = 'SELECT ID,Account.Name,AccountId,Course_Sections__c,Course_Section_ID__c,Account.Faculty_Employment_Status__pc,Account.CPS_faculty_type__pc,Campus__c,Academic_Year__c,Term__c,Account.Union_Member__c,Account.NU_Id__c,Status From Contract WHERE ' ;
query2=query2+'RecordType.Id IN('+'\''+strFacultyTeachingAssignment+'\''+')';
query2=query2+'AND Academic_Year__c IN ('+result+')';
query2=query2+'AND Status = \'Activated\'';
query2=query2+'AND Account.Union_Member__c = True ';
query2=query2+'AND Account.Faculty_Employment_Status__pc = \'Active\'';
query2=query2+'AND Account.CPS_faculty_type__pc NOT IN ( \'Full Time Faculty\',\'Half Time Faculty\') ';
query2=query2+'AND (Campus__c Like '+inputCampus1+'OR Campus__c Like '+inputCampus2+'OR Campus__c Like '+inputCampus3+')';
List<Contract> activeContractLst = Database.query(query2);
List<Id> contractLst = new List<Id>();
for(Contract c2 : activeContractLst) {
contractLst.add(c2.Id);
}
for(Contract c1 : cancContractLst) {
contractLst.add(c1.Id);
}
List<Faculty_Good_Faith_Consideration__c> facLst = [Select id,name,Contract__c,Course_Section__r.Course_Section_Status__c from Faculty_Good_Faith_Consideration__c where Contract__c IN : contractLst];
List<Id> idList = new List<Id>();
List<Faculty_Good_Faith_Consideration__c> updateAboutlst = new List<Faculty_Good_Faith_Consideration__c>();
for(Faculty_Good_Faith_Consideration__c fac : facLst) {
if(fac.Course_Section__r.Course_Section_Status__c == 'Cancelled') {
Faculty_Good_Faith_Consideration__c gc = new Faculty_Good_Faith_Consideration__c(id=fac.Id);
gc.Accepted_Buyout__c = true;
gc.GFC_For_Given_Course__c = false;
updateAboutlst.add(gc);
}
idList.add(fac.Contract__c);
}
if(updateAboutlst.size()>0)
update updateAboutlst;
Set<Id> newConSet = new Set<Id>(contractLst);
Set<Id> exConSet = new Set<Id>(idList);
List<Id> dupes = new List<Id>();
for(Id ct : newConSet) {
if(exConSet.contains(ct)) {
dupes.add(ct);
}
}
newConSet.removeAll(dupes);
String query = 'Select id,Account.Name,AccountId,Course_Sections__c,Course_Section_Status__c,Course_Section_ID__c,Account.Faculty_Employment_Status__pc,Account.CPS_faculty_type__pc,Campus__c,Academic_Year__c,Term__c,Account.Union_Member__c,Account.NU_Id__c,Cancelation_Fee_Scheduled__c,Canceled_Contract_Fee__c,Status,Compensation_Rate__r.name from Contract where ID IN';
query = query + ' :newConSet';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc, List<Contract> scope)
{
list<Contract> updateContractList = new list<Contract>();
list<Faculty_Good_Faith_Consideration__c> gfcLst = new List<Faculty_Good_Faith_Consideration__c>();
List<Faculty_Good_Faith_Consideration__c> updategfcLst = new List<Faculty_Good_Faith_Consideration__c>();
Map<id,String> gfcCancelled = new Map<id,String>();
for(Contract Con : scope)
{
if(con.Status == 'Activated'){
Faculty_Good_Faith_Consideration__c facl = new Faculty_Good_Faith_Consideration__c();
facl.Name = con.Account.Name ;
facl.Contract__c = con.Id;
facl.Course_Section__c = con.Course_Section_ID__c;
facl.AccountName__c = con.AccountId;
facl.GFC_For_Given_Course__c = True;
gfcLst.add(facl);
}
if(con.Course_Section_Status__c == 'Cancelled') {
gfcCancelled.put(con.Id,con.Course_Section_ID__c);
}
}
try {
if(gfcLst.size()>0)
insert gfcLst;
if(gfcCancelled.size()>0) {
List<Faculty_Good_Faith_Consideration__c> gfcCancLst = [Select id,name,Course_Section__r.Course_Section_Status__c from Faculty_Good_Faith_Consideration__c where Course_Section__c IN :gfcCancelled.values()];
for(Faculty_Good_Faith_Consideration__c gfcCanc: gfcCancLst) {
Faculty_Good_Faith_Consideration__c gf = new Faculty_Good_Faith_Consideration__c(id = gfcCanc.Id);
gf.Accepted_Buyout__c = True;
gf.GFC_For_Given_Course__c = false;
updategfcLst.add(gf);
}
}
if(updategfcLst.size()>0) {
database.SaveResult[] srlist = database.update(updategfcLst,false);
for(database.SaveResult sr : srlist)
{
if(sr.isSuccess())
{
system.debug('Successfully updated GFC Records:'+sr.getId());
}
else
{
for(database.Error err : sr.getErrors())
{
system.debug(err.getStatusCode()+':'+err.getMessage());
}
}
}
}
}catch(Exception e){
System.debug(e.getMessage());
}
}
global void finish(Database.BatchableContext BC)
{
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------
Test Class:
@isTest
public class FacultyearnGFCBatchTest {
@TestSetup
public static void setup() {
Id CPS_ACCOUNT_REC_TYPE = NEUSFIDHelper.retrieveRecordTypeId(CPSConstants.REC_TYPE_ACCOUNT_APPLICANT, 'Account');
Id TEACHING_ASSIGNMENT_REC_TYPE = NEUSFIDHelper.retrieveRecordTypeId(CPSConstants.FACULTY_TEACHING_ASSIGNMENT_RECORD_TYPE_NAME, 'Contract');
Account acc = new Account(FirstName = 'Novak', LastName = 'Djokovic', CPS_faculty_type__pc = 'Lecturer' ,Faculty_Employment_Status__pc ='Active', Union_Member__c = true, NU_ID__c = '1112229', RecordTypeId = CPS_ACCOUNT_REC_TYPE);
insert acc;
Term__c term =
new Term__c(
Name = 'Winter 2022 CPS Quarter',
Term_External_Id__c = '006A',
Academic_Year__c = '2022');
insert term;
Part_of_Term__c partofTerms =
new Part_of_Term__c(
Name = '8',
Term__c = term.Id,
Short_Name__c = 'Spring 2022, First Six-week Session',
Part_of_Term_External_Id__c = 'A10008',
Start_Date_Part_of_Term__c = Date.parse('4/1/2022'));
insert partofTerms;
Course_Catalog__c courseCatalog =
new Course_Catalog__c(Name = 'cat-1');
insert courseCatalog;
Course_Section__c cSec = new Course_Section__c(
Name = '202215-80001',
CRN__c = '80001',
Course_Catalog__c = courseCatalog.id,
Part_of_Term__c = partofTerms.Id,
Credit_Hours__c = 3,
Payroll_Actual_Enrollment__c = 0,
Course_Section_Status__c = 'Active');
insert cSec;
Compensation_Rate__c compensationRate =
new Compensation_Rate__c(
Name = 'No Payment for this Class',
RecordTypeId = CPSCompensationRateUtil.getRecordTypeId('Base Contract Pay'),
Rate_Type__c = 'Flat Amount',
Amount__c = 8000);
insert compensationRate;
Contract contract =null;
contract = new Contract(
AccountId = acc.Id,
Academic_Year__c = '2018-2019',
Is_Canceled__c = false,
StartDate = Date.newInstance(2018, 10, 10),
RecordTypeId = TEACHING_ASSIGNMENT_REC_TYPE,
Course_Section_ID__c = cSec.id,
Type__c = 'Primary',
Status = 'Draft',
Campus__c = 'Boston',
Canceled_Contract_Fee__c = '100% GFC Fee',
Compensation_Rate__c = compensationRate.id);
insert contract;
Course_Section__c cs = new Course_Section__c(Id=cSec.Id);
cs.Course_Section_Status__c ='Cancelled';
update cs;
Contract con = new Contract(Id = contract.Id);
con.Status ='Activated';
update con;
Faculty_Good_Faith_Consideration__c fac = new Faculty_Good_Faith_Consideration__c();
fac.Contract__c = con.Id;
fac.Course_Section__c = con.Course_Section_ID__c;
fac.AccountName__c = con.AccountId;
fac.GFC_For_Given_Course__c = true;
insert fac;
}
public static testmethod void test_GFCexecute() {
// Implement test code
List<Contract> conLst = new List<Contract>();
Database.BatchableContext bc;
Test.startTest();
try {
conLst =[Select Id,name,Status,Course_Section_Status__c,Course_Section_ID__c,AccountId from Contract];
FacultyearnGFCBatch gfcBatch = new FacultyearnGFCBatch();
Database.executeBatch(gfcBatch);
gfcBatch.execute(bc,conLst);
} catch(Exception ex) {
System.debug(ex.getMessage());
}
Test.stopTest();
System.assert(conLst[0].Status=='Activated');
System.assert(conLst[0].Course_Section_Status__c=='Cancelled');
}
public static testmethod void testStart() {
List<Faculty_Good_Faith_Consideration__c> facl = new List<Faculty_Good_Faith_Consideration__c>();
Database.BatchableContext bc;
Test.startTest();
facl =[Select id,name,Course_Section__r.Course_Section_Status__c from Faculty_Good_Faith_Consideration__c];
FacultyearnGFCBatch gfcBatch = new FacultyearnGFCBatch();
gfcBatch.start(bc);
Test.stopTest();
System.assert(facl[0].Id!=null);
}
}
Class:
global class FacultyearnGFCBatch implements Database.Batchable<sObject>
{
global Database.QueryLocator start(Database.BatchableContext bc)
{
integer year = system.today().year();
string str1 = (year-1)+'-'+(year);
string Campus1 = 'Boston';
string Campus2 = 'BRD';
string Campus3 = 'BOS';
string inputCampus1 = '\''+'%'+ Campus1+'%'+'\'';
string inputCampus2 = '\''+'%'+ Campus2+'%'+'\'' ;
string inputCampus3 = '\''+'%'+ Campus3+'%'+'\'' ;
String[] Str = new string[]{str1};
string result = '\''+String.join(Str,'\',\'')+'\'';
string Class1 = 'No Payment for this Class';
string Class2 = '\''+'%'+ Class1+'%'+'\'';
Id strFacultyTeachingAssignment = NEUSFIDHelper.retrieveRecordTypeId(APIPConstants.REC_TYPE_FACULTY_TEACHING_ASSIGNMENT, 'Contract');
string query1 = 'SELECT id,Account.Name,Cancelation_Fee_Scheduled__c,Canceled_Contract_Fee__c,Course_Section_ID__r.course_section_status__c,Compensation_Rate__r.name,Course_Section_ID__c,AccountId FROM Contract WHERE ';
query1=query1+'RecordType.Id IN('+'\''+strFacultyTeachingAssignment+'\''+')';
query1=query1+'AND Cancelation_Fee_Scheduled__c = True ';
query1=query1+'AND Course_Section_ID__r.course_section_status__c = \'Cancelled\' ';
query1=query1+'AND Canceled_Contract_Fee__c IN ( \'100% GFC Fee\',\'75% GFC Fee\') ';
query1=query1+'AND (NOT Compensation_Rate__r.name like '+Class2+')';
List<Contract> cancContractLst = Database.query(query1);
string query2 = 'SELECT ID,Account.Name,AccountId,Course_Sections__c,Course_Section_ID__c,Account.Faculty_Employment_Status__pc,Account.CPS_faculty_type__pc,Campus__c,Academic_Year__c,Term__c,Account.Union_Member__c,Account.NU_Id__c,Status From Contract WHERE ' ;
query2=query2+'RecordType.Id IN('+'\''+strFacultyTeachingAssignment+'\''+')';
query2=query2+'AND Academic_Year__c IN ('+result+')';
query2=query2+'AND Status = \'Activated\'';
query2=query2+'AND Account.Union_Member__c = True ';
query2=query2+'AND Account.Faculty_Employment_Status__pc = \'Active\'';
query2=query2+'AND Account.CPS_faculty_type__pc NOT IN ( \'Full Time Faculty\',\'Half Time Faculty\') ';
query2=query2+'AND (Campus__c Like '+inputCampus1+'OR Campus__c Like '+inputCampus2+'OR Campus__c Like '+inputCampus3+')';
List<Contract> activeContractLst = Database.query(query2);
List<Id> contractLst = new List<Id>();
for(Contract c2 : activeContractLst) {
contractLst.add(c2.Id);
}
for(Contract c1 : cancContractLst) {
contractLst.add(c1.Id);
}
List<Faculty_Good_Faith_Consideration__c> facLst = [Select id,name,Contract__c,Course_Section__r.Course_Section_Status__c from Faculty_Good_Faith_Consideration__c where Contract__c IN : contractLst];
List<Id> idList = new List<Id>();
List<Faculty_Good_Faith_Consideration__c> updateAboutlst = new List<Faculty_Good_Faith_Consideration__c>();
for(Faculty_Good_Faith_Consideration__c fac : facLst) {
if(fac.Course_Section__r.Course_Section_Status__c == 'Cancelled') {
Faculty_Good_Faith_Consideration__c gc = new Faculty_Good_Faith_Consideration__c(id=fac.Id);
gc.Accepted_Buyout__c = true;
gc.GFC_For_Given_Course__c = false;
updateAboutlst.add(gc);
}
idList.add(fac.Contract__c);
}
if(updateAboutlst.size()>0)
update updateAboutlst;
Set<Id> newConSet = new Set<Id>(contractLst);
Set<Id> exConSet = new Set<Id>(idList);
List<Id> dupes = new List<Id>();
for(Id ct : newConSet) {
if(exConSet.contains(ct)) {
dupes.add(ct);
}
}
newConSet.removeAll(dupes);
String query = 'Select id,Account.Name,AccountId,Course_Sections__c,Course_Section_Status__c,Course_Section_ID__c,Account.Faculty_Employment_Status__pc,Account.CPS_faculty_type__pc,Campus__c,Academic_Year__c,Term__c,Account.Union_Member__c,Account.NU_Id__c,Cancelation_Fee_Scheduled__c,Canceled_Contract_Fee__c,Status,Compensation_Rate__r.name from Contract where ID IN';
query = query + ' :newConSet';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc, List<Contract> scope)
{
list<Contract> updateContractList = new list<Contract>();
list<Faculty_Good_Faith_Consideration__c> gfcLst = new List<Faculty_Good_Faith_Consideration__c>();
List<Faculty_Good_Faith_Consideration__c> updategfcLst = new List<Faculty_Good_Faith_Consideration__c>();
Map<id,String> gfcCancelled = new Map<id,String>();
for(Contract Con : scope)
{
if(con.Status == 'Activated'){
Faculty_Good_Faith_Consideration__c facl = new Faculty_Good_Faith_Consideration__c();
facl.Name = con.Account.Name ;
facl.Contract__c = con.Id;
facl.Course_Section__c = con.Course_Section_ID__c;
facl.AccountName__c = con.AccountId;
facl.GFC_For_Given_Course__c = True;
gfcLst.add(facl);
}
if(con.Course_Section_Status__c == 'Cancelled') {
gfcCancelled.put(con.Id,con.Course_Section_ID__c);
}
}
try {
if(gfcLst.size()>0)
insert gfcLst;
if(gfcCancelled.size()>0) {
List<Faculty_Good_Faith_Consideration__c> gfcCancLst = [Select id,name,Course_Section__r.Course_Section_Status__c from Faculty_Good_Faith_Consideration__c where Course_Section__c IN :gfcCancelled.values()];
for(Faculty_Good_Faith_Consideration__c gfcCanc: gfcCancLst) {
Faculty_Good_Faith_Consideration__c gf = new Faculty_Good_Faith_Consideration__c(id = gfcCanc.Id);
gf.Accepted_Buyout__c = True;
gf.GFC_For_Given_Course__c = false;
updategfcLst.add(gf);
}
}
if(updategfcLst.size()>0) {
database.SaveResult[] srlist = database.update(updategfcLst,false);
for(database.SaveResult sr : srlist)
{
if(sr.isSuccess())
{
system.debug('Successfully updated GFC Records:'+sr.getId());
}
else
{
for(database.Error err : sr.getErrors())
{
system.debug(err.getStatusCode()+':'+err.getMessage());
}
}
}
}
}catch(Exception e){
System.debug(e.getMessage());
}
}
global void finish(Database.BatchableContext BC)
{
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------
Test Class:
@isTest
public class FacultyearnGFCBatchTest {
@TestSetup
public static void setup() {
Id CPS_ACCOUNT_REC_TYPE = NEUSFIDHelper.retrieveRecordTypeId(CPSConstants.REC_TYPE_ACCOUNT_APPLICANT, 'Account');
Id TEACHING_ASSIGNMENT_REC_TYPE = NEUSFIDHelper.retrieveRecordTypeId(CPSConstants.FACULTY_TEACHING_ASSIGNMENT_RECORD_TYPE_NAME, 'Contract');
Account acc = new Account(FirstName = 'Novak', LastName = 'Djokovic', CPS_faculty_type__pc = 'Lecturer' ,Faculty_Employment_Status__pc ='Active', Union_Member__c = true, NU_ID__c = '1112229', RecordTypeId = CPS_ACCOUNT_REC_TYPE);
insert acc;
Term__c term =
new Term__c(
Name = 'Winter 2022 CPS Quarter',
Term_External_Id__c = '006A',
Academic_Year__c = '2022');
insert term;
Part_of_Term__c partofTerms =
new Part_of_Term__c(
Name = '8',
Term__c = term.Id,
Short_Name__c = 'Spring 2022, First Six-week Session',
Part_of_Term_External_Id__c = 'A10008',
Start_Date_Part_of_Term__c = Date.parse('4/1/2022'));
insert partofTerms;
Course_Catalog__c courseCatalog =
new Course_Catalog__c(Name = 'cat-1');
insert courseCatalog;
Course_Section__c cSec = new Course_Section__c(
Name = '202215-80001',
CRN__c = '80001',
Course_Catalog__c = courseCatalog.id,
Part_of_Term__c = partofTerms.Id,
Credit_Hours__c = 3,
Payroll_Actual_Enrollment__c = 0,
Course_Section_Status__c = 'Active');
insert cSec;
Compensation_Rate__c compensationRate =
new Compensation_Rate__c(
Name = 'No Payment for this Class',
RecordTypeId = CPSCompensationRateUtil.getRecordTypeId('Base Contract Pay'),
Rate_Type__c = 'Flat Amount',
Amount__c = 8000);
insert compensationRate;
Contract contract =null;
contract = new Contract(
AccountId = acc.Id,
Academic_Year__c = '2018-2019',
Is_Canceled__c = false,
StartDate = Date.newInstance(2018, 10, 10),
RecordTypeId = TEACHING_ASSIGNMENT_REC_TYPE,
Course_Section_ID__c = cSec.id,
Type__c = 'Primary',
Status = 'Draft',
Campus__c = 'Boston',
Canceled_Contract_Fee__c = '100% GFC Fee',
Compensation_Rate__c = compensationRate.id);
insert contract;
Course_Section__c cs = new Course_Section__c(Id=cSec.Id);
cs.Course_Section_Status__c ='Cancelled';
update cs;
Contract con = new Contract(Id = contract.Id);
con.Status ='Activated';
update con;
Faculty_Good_Faith_Consideration__c fac = new Faculty_Good_Faith_Consideration__c();
fac.Contract__c = con.Id;
fac.Course_Section__c = con.Course_Section_ID__c;
fac.AccountName__c = con.AccountId;
fac.GFC_For_Given_Course__c = true;
insert fac;
}
public static testmethod void test_GFCexecute() {
// Implement test code
List<Contract> conLst = new List<Contract>();
Database.BatchableContext bc;
Test.startTest();
try {
conLst =[Select Id,name,Status,Course_Section_Status__c,Course_Section_ID__c,AccountId from Contract];
FacultyearnGFCBatch gfcBatch = new FacultyearnGFCBatch();
Database.executeBatch(gfcBatch);
gfcBatch.execute(bc,conLst);
} catch(Exception ex) {
System.debug(ex.getMessage());
}
Test.stopTest();
System.assert(conLst[0].Status=='Activated');
System.assert(conLst[0].Course_Section_Status__c=='Cancelled');
}
public static testmethod void testStart() {
List<Faculty_Good_Faith_Consideration__c> facl = new List<Faculty_Good_Faith_Consideration__c>();
Database.BatchableContext bc;
Test.startTest();
facl =[Select id,name,Course_Section__r.Course_Section_Status__c from Faculty_Good_Faith_Consideration__c];
FacultyearnGFCBatch gfcBatch = new FacultyearnGFCBatch();
gfcBatch.start(bc);
Test.stopTest();
System.assert(facl[0].Id!=null);
}
}
@@@we@@@
Can you Show Screen Shot your Cover code