You need to sign in to do that
Don't have an account?
Abhinav_SFDC
Kindly help me to achieve more than 75% code coverage of this batch class
i am new in coding .
global class Batch_InvoiceCreation implements Database.Batchable<sObject>,Schedulable {
global String query;
global Batch_InvoiceCreation() {
}
global void execute(SchedulableContext sc){
Database.executebatch(this);
}
global Database.QueryLocator start(Database.BatchableContext BC) {
date d = (date)date.today().addDays(30);
query='SELECT Account__c,Ageing__c,AmountAdjusted__c,AmountSettled__c,Amount__c,'+
'Applicable_Tax__c,Bad_Debts__c,Contracts_Billing_Entity__c,Contracts_Country__c,Contract__c,Conversion_Rate_To_USD__c,'+
'Conversion_Rate__c,CreatedById,CreatedDate,Credit_Days__c,Currency__c,Frequency__c,From__c,InvoiceDate__c,InvoiceNumber__c,'+
'Invoice_Amount_in_INR__c,Invoice_Amount__c,Invoice_Billing_Entity__c,Invoice_Cancelled__c,Invoice_Completed_Date_Reference__c,'+
'Invoice_Completed_Date__c,Invoice_Due_Date_Reference__c,Invoice_Due_Date__c,Invoi__c,IsDeleted,LastActivityDate,LastModifiedById,'+
'LastModifiedDate,LastReferencedDate,LastViewedDate,Last_Activity_Description__c,Monthsbtw_inv__c,Month__c,Name,No_of_of_months__c,'+
'Opportunity__c,Outstanding_Amount__c,OwnerId,Payment_Status__c,Product_Purchased__c,Property_Address__c,Property_Name__c,'+
'Reason_for_Cancellation__c,Sales_Owner__c,Sales_Price_in_INR__c,Settlement_Count__c,Status_Reference__c,Status__c,'+
'Subscritption_end_date__c,Subscritption_start_date__c,SystemModstamp,Total_Amount_in_INR_formula__c,Batch_Invoice_Number__c,Is_Auto_Invoice__c,'+
'Total_Amount_in_USD_formula__c,Total_Amount_in_USD__c,Total_Amount__c,To__c,Type__c,Vertical__c,Year__c FROM Invoice__c WHERE (Frequency__c =\'Yearly\' OR Frequency__c =\'Quarterly\' OR Frequency__c =\'Half yearly\') AND '+
'Contract__r.Auto_Renewal__c =\'Yes\' AND Vertical__c=\'Hospi\'';
query=query+'AND To__c='+string.valueof(d).substring(0,10);//NEXT_MONTHAND Type__c=\'Fresh\' ';
//Quarterly
//Half yearly
system.debug('TEST 2------------->'+query);
system.debug('TEST BP------------->'+Database.getQueryLocator(query));
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Invoice__c> scope) {
List<Invoice__c> listInvoiceClone = (List<Invoice__c>)scope;
List<Invoice__c> listInsertRecords= new List<Invoice__c>() ;
List<Invoice__c> listInsertRecordCl= new List<Invoice__c>() ;
List<Invoice__c> listUpdatRecords= new List<Invoice__c>() ;
Invoice__c inCreate= new Invoice__c() ;
listInsertRecordCl=listInvoiceClone.deepClone();
set<string> setInvoicId= new set<string>();
Map<string ,List<description__c>> MapDescrpt = new Map<string, List<description__c>>();
for(Invoice__c inObj:listInvoiceClone) {
setInvoicId.add(inObj.id);
}
List<Description__c> ListDescription=[select Id,Description__c,From__c,Invoice__c,Invoice__r.name,Sr_No__c,To__c,Amount__c from Description__c where Invoice__c IN: setInvoicId];
for(Description__c desObj:ListDescription) {
if(MapDescrpt.containskey(desObj.Invoice__r.name)) {
MapDescrpt.get(desObj.Invoice__r.name).add(desObj);
}
else {
MapDescrpt.put(desObj.Invoice__r.name,new List<Description__c>());
MapDescrpt.get(desObj.Invoice__r.name).add(desObj);
}
}
system.debug('listInsertRecordCl.size()'+listInsertRecordCl.size());
system.debug('MapDescrpt)'+MapDescrpt);
for(integer j=0;j<listInsertRecordCl.size();j++ ) {
if(listInsertRecordCl[j].Frequency__c=='Yearly') {
listInsertRecordCl[j].To__c =listInsertRecordCl[j].To__c.addDays(365);
listInsertRecordCl[j].From__c =listInsertRecordCl[j].From__c.addDays(365);
}
else if(listInsertRecordCl[j].Frequency__c=='Half yearly') {
listInsertRecordCl[j].To__c =listInsertRecordCl[j].To__c.addDays(182);
listInsertRecordCl[j].From__c =listInsertRecordCl[j].From__c.addDays(182);
}
else if(listInsertRecordCl[j].Frequency__c=='Quarterly') {
listInsertRecordCl[j].To__c =listInsertRecordCl[j].To__c.addDays(91);
listInsertRecordCl[j].From__c =listInsertRecordCl[j].From__c.addDays(91);
}
listInsertRecordCl[j].InvoiceNumber__c = listInsertRecordCl[j].InvoiceNumber__c+j;
listInsertRecordCl[j].Batch_Invoice_Number__c =listInvoiceClone[j].Name;
//listInsertRecordCl[j].Is_Auto_Invoice__c =true;
listInsertRecordCl[j].InvoiceDate__c =system.Today();
listInsertRecordCl[j].Type__c ='Renewal';
listInsertRecords.add(listInsertRecordCl[j]);
system.debug('TESTqqq------->'+listInsertRecords);
}
List<Description__c> listInvoceDescriptionInst= new List<Description__c> ();
if(listInsertRecords.size() > 0) {
system.debug('TEST------->'+listInsertRecords);
insert listInsertRecords;
for(Invoice__c inObj:listInsertRecords) {
if(MapDescrpt.containskey(inObj.Batch_Invoice_Number__c)) {
for(Description__c desObj:MapDescrpt.get(inObj.Batch_Invoice_Number__c)) {
if(!desObj.Description__c.contains('Setup Fee')) {
Description__c DesObj1 = new Description__c();
DesObj1.Description__c =desObj.Description__c;
DesObj1.Invoice__c=inObj.id;
DesObj1.Amount__c=desObj.Amount__c;
DesObj1.Sr_No__c =desObj.Sr_No__c;
listInvoceDescriptionInst.add(DesObj1);
}
}
}
}
system.debug('TEST GAU------>'+listInvoceDescriptionInst);
if(listInvoceDescriptionInst.size() > 0) {
insert listInvoceDescriptionInst;
for(Invoice__c inObj1:listInsertRecords) {
inObj1.Is_Auto_Invoice__c =true;
listUpdatRecords.add(inObj1);
}
if(listUpdatRecords.size() > 0) {
update listUpdatRecords;
}
}
}
}
global void finish(Database.BatchableContext BC) {
}
}
=============================================================================================================
this is my test class===>
@isTest
private class Test_InvoiceUpdate {
static TestMethod void Test_BatchInvoiceCreation() {
Database.QueryLocator QL;
Database.BatchableContext BC;
SchedulableContext SC;
List<Invoice__c> InvoiceList = new List<Invoice__c>();
List<Description__c> DesList=new List<Description__c>();
Batch_InvoiceCreation AU = new Batch_InvoiceCreation ();
QL = AU.start(bc);
Database.QueryLocatorIterator QIT = QL.iterator();
AU.execute(SC);
AU.finish(BC);
Account acc=new Account ();
acc.name='DummyTest';
Insert acc;
Contract Contr=new Contract();
Contr.AccountId=acc.Id;
Contr.Status='Draft';
Insert Contr;
Invoice__c Invc=new Invoice__c();
Invc.Contract__c=Contr.id;
Invc.Vertical__c='PS';
Invc.InvoiceDate__c=system.today();
Invc.Credit_Days__c=10;
Invc.Currency__c='USD';
Invc.Frequency__c='Yearly';
Invc.Conversion_Rate__c=100;
Invc.Is_Auto_Invoice__c = true;
Invc.Conversion_Rate_To_USD__c=1.000000;
Invc.Type__c='Fresh';
Invc.Property_Name__c='TestMon';
Invc.From__c=system.today()-20;
Invc.To__c=system.today();
insert Invc;
System.debug('Invc-------->'+Invc);
InvoiceList.add(Invc);
AU.execute(BC, InvoiceList );
System.debug('InvoiceList-------->'+InvoiceList);
Invoice__c Invc1=new Invoice__c();
Invc1.Contract__c=Contr.id;
Invc1.Vertical__c='PS';
Invc1.InvoiceDate__c=system.today();
Invc1.Credit_Days__c=10;
Invc1.Currency__c='USD';
Invc1.Batch_Invoice_Number__c = 'abhi 01';
Invc1.Frequency__c='Half yearly';
Invc1.Conversion_Rate__c=100;
Invc1.Is_Auto_Invoice__c = true;
Invc1.Conversion_Rate_To_USD__c=1.000000;
Invc1.Type__c='Fresh';
Invc1.Property_Name__c='TestMon';
Invc1.From__c=system.today()-20;
Invc1.To__c=system.today();
insert Invc1;
System.debug('Invc-------->'+Invc);
InvoiceList.add(Invc1);
AU.execute(BC, InvoiceList );
System.debug('InvoiceList-------->'+InvoiceList);
Invoice__c Invc2=new Invoice__c();
Invc2.Contract__c=Contr.id;
Invc2.Vertical__c='PS';
Invc2.InvoiceDate__c=system.today();
Invc2.Credit_Days__c=10;
Invc2.Currency__c='USD';
Invc2.Frequency__c='Quarterly';
Invc2.Conversion_Rate__c=100;
Invc2.Conversion_Rate_To_USD__c=1.000000;
Invc2.Type__c='Fresh';
Invc2.Is_Auto_Invoice__c = true;
Invc2.Property_Name__c='TestMon';
Invc2.From__c=system.today()-20;
Invc2.To__c=system.today();
insert Invc2;
System.debug('Invc-------->'+Invc);
InvoiceList.add(Invc2);
AU.execute(BC, InvoiceList );
System.debug('InvoiceList-------->'+InvoiceList);
list<Invoice__c> i = [select id, name from Invoice__c where id = :InvoiceList];
Description__c DesObj=new Description__c();
DesObj.Invoice__c=Invc.id;
DesObj.Description__c = 'Setup fee';
DesObj.Sr_No__c=10;
Insert DesObj;
DesList.add(DesObj);
System.debug('DesList-------->'+DesList);
Description__c DesObj2=new Description__c();
DesObj2.Invoice__c=Invc2.id;
DesObj2.Description__c = 'Half yearly subscription';
DesObj2.Sr_No__c=10;
DesObj2.Amount__c=1234.00;
Insert DesObj2;
DesList.add(DesObj2);
System.debug('DesList-------->'+DesList);
Description__c DesObj1=new Description__c();
DesObj1.Invoice__c=Invc1.id;
DesObj1.Description__c = 'Half yearly subscription';
DesObj1.Sr_No__c=10;
DesObj1.Amount__c=1234.00;
Insert DesObj1;
DesList.add(DesObj1);
System.debug('DesList-------->'+DesList);
Test.startTest();
Batch_InvoiceCreation BatchInv=new Batch_InvoiceCreation ();
ID batchprocessid = Database.executeBatch(BatchInv);
Test.stopTest();
}
}
global class Batch_InvoiceCreation implements Database.Batchable<sObject>,Schedulable {
global String query;
global Batch_InvoiceCreation() {
}
global void execute(SchedulableContext sc){
Database.executebatch(this);
}
global Database.QueryLocator start(Database.BatchableContext BC) {
date d = (date)date.today().addDays(30);
query='SELECT Account__c,Ageing__c,AmountAdjusted__c,AmountSettled__c,Amount__c,'+
'Applicable_Tax__c,Bad_Debts__c,Contracts_Billing_Entity__c,Contracts_Country__c,Contract__c,Conversion_Rate_To_USD__c,'+
'Conversion_Rate__c,CreatedById,CreatedDate,Credit_Days__c,Currency__c,Frequency__c,From__c,InvoiceDate__c,InvoiceNumber__c,'+
'Invoice_Amount_in_INR__c,Invoice_Amount__c,Invoice_Billing_Entity__c,Invoice_Cancelled__c,Invoice_Completed_Date_Reference__c,'+
'Invoice_Completed_Date__c,Invoice_Due_Date_Reference__c,Invoice_Due_Date__c,Invoi__c,IsDeleted,LastActivityDate,LastModifiedById,'+
'LastModifiedDate,LastReferencedDate,LastViewedDate,Last_Activity_Description__c,Monthsbtw_inv__c,Month__c,Name,No_of_of_months__c,'+
'Opportunity__c,Outstanding_Amount__c,OwnerId,Payment_Status__c,Product_Purchased__c,Property_Address__c,Property_Name__c,'+
'Reason_for_Cancellation__c,Sales_Owner__c,Sales_Price_in_INR__c,Settlement_Count__c,Status_Reference__c,Status__c,'+
'Subscritption_end_date__c,Subscritption_start_date__c,SystemModstamp,Total_Amount_in_INR_formula__c,Batch_Invoice_Number__c,Is_Auto_Invoice__c,'+
'Total_Amount_in_USD_formula__c,Total_Amount_in_USD__c,Total_Amount__c,To__c,Type__c,Vertical__c,Year__c FROM Invoice__c WHERE (Frequency__c =\'Yearly\' OR Frequency__c =\'Quarterly\' OR Frequency__c =\'Half yearly\') AND '+
'Contract__r.Auto_Renewal__c =\'Yes\' AND Vertical__c=\'Hospi\'';
query=query+'AND To__c='+string.valueof(d).substring(0,10);//NEXT_MONTHAND Type__c=\'Fresh\' ';
//Quarterly
//Half yearly
system.debug('TEST 2------------->'+query);
system.debug('TEST BP------------->'+Database.getQueryLocator(query));
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Invoice__c> scope) {
List<Invoice__c> listInvoiceClone = (List<Invoice__c>)scope;
List<Invoice__c> listInsertRecords= new List<Invoice__c>() ;
List<Invoice__c> listInsertRecordCl= new List<Invoice__c>() ;
List<Invoice__c> listUpdatRecords= new List<Invoice__c>() ;
Invoice__c inCreate= new Invoice__c() ;
listInsertRecordCl=listInvoiceClone.deepClone();
set<string> setInvoicId= new set<string>();
Map<string ,List<description__c>> MapDescrpt = new Map<string, List<description__c>>();
for(Invoice__c inObj:listInvoiceClone) {
setInvoicId.add(inObj.id);
}
List<Description__c> ListDescription=[select Id,Description__c,From__c,Invoice__c,Invoice__r.name,Sr_No__c,To__c,Amount__c from Description__c where Invoice__c IN: setInvoicId];
for(Description__c desObj:ListDescription) {
if(MapDescrpt.containskey(desObj.Invoice__r.name)) {
MapDescrpt.get(desObj.Invoice__r.name).add(desObj);
}
else {
MapDescrpt.put(desObj.Invoice__r.name,new List<Description__c>());
MapDescrpt.get(desObj.Invoice__r.name).add(desObj);
}
}
system.debug('listInsertRecordCl.size()'+listInsertRecordCl.size());
system.debug('MapDescrpt)'+MapDescrpt);
for(integer j=0;j<listInsertRecordCl.size();j++ ) {
if(listInsertRecordCl[j].Frequency__c=='Yearly') {
listInsertRecordCl[j].To__c =listInsertRecordCl[j].To__c.addDays(365);
listInsertRecordCl[j].From__c =listInsertRecordCl[j].From__c.addDays(365);
}
else if(listInsertRecordCl[j].Frequency__c=='Half yearly') {
listInsertRecordCl[j].To__c =listInsertRecordCl[j].To__c.addDays(182);
listInsertRecordCl[j].From__c =listInsertRecordCl[j].From__c.addDays(182);
}
else if(listInsertRecordCl[j].Frequency__c=='Quarterly') {
listInsertRecordCl[j].To__c =listInsertRecordCl[j].To__c.addDays(91);
listInsertRecordCl[j].From__c =listInsertRecordCl[j].From__c.addDays(91);
}
listInsertRecordCl[j].InvoiceNumber__c = listInsertRecordCl[j].InvoiceNumber__c+j;
listInsertRecordCl[j].Batch_Invoice_Number__c =listInvoiceClone[j].Name;
//listInsertRecordCl[j].Is_Auto_Invoice__c =true;
listInsertRecordCl[j].InvoiceDate__c =system.Today();
listInsertRecordCl[j].Type__c ='Renewal';
listInsertRecords.add(listInsertRecordCl[j]);
system.debug('TESTqqq------->'+listInsertRecords);
}
List<Description__c> listInvoceDescriptionInst= new List<Description__c> ();
if(listInsertRecords.size() > 0) {
system.debug('TEST------->'+listInsertRecords);
insert listInsertRecords;
for(Invoice__c inObj:listInsertRecords) {
if(MapDescrpt.containskey(inObj.Batch_Invoice_Number__c)) {
for(Description__c desObj:MapDescrpt.get(inObj.Batch_Invoice_Number__c)) {
if(!desObj.Description__c.contains('Setup Fee')) {
Description__c DesObj1 = new Description__c();
DesObj1.Description__c =desObj.Description__c;
DesObj1.Invoice__c=inObj.id;
DesObj1.Amount__c=desObj.Amount__c;
DesObj1.Sr_No__c =desObj.Sr_No__c;
listInvoceDescriptionInst.add(DesObj1);
}
}
}
}
system.debug('TEST GAU------>'+listInvoceDescriptionInst);
if(listInvoceDescriptionInst.size() > 0) {
insert listInvoceDescriptionInst;
for(Invoice__c inObj1:listInsertRecords) {
inObj1.Is_Auto_Invoice__c =true;
listUpdatRecords.add(inObj1);
}
if(listUpdatRecords.size() > 0) {
update listUpdatRecords;
}
}
}
}
global void finish(Database.BatchableContext BC) {
}
}
=============================================================================================================
this is my test class===>
@isTest
private class Test_InvoiceUpdate {
static TestMethod void Test_BatchInvoiceCreation() {
Database.QueryLocator QL;
Database.BatchableContext BC;
SchedulableContext SC;
List<Invoice__c> InvoiceList = new List<Invoice__c>();
List<Description__c> DesList=new List<Description__c>();
Batch_InvoiceCreation AU = new Batch_InvoiceCreation ();
QL = AU.start(bc);
Database.QueryLocatorIterator QIT = QL.iterator();
AU.execute(SC);
AU.finish(BC);
Account acc=new Account ();
acc.name='DummyTest';
Insert acc;
Contract Contr=new Contract();
Contr.AccountId=acc.Id;
Contr.Status='Draft';
Insert Contr;
Invoice__c Invc=new Invoice__c();
Invc.Contract__c=Contr.id;
Invc.Vertical__c='PS';
Invc.InvoiceDate__c=system.today();
Invc.Credit_Days__c=10;
Invc.Currency__c='USD';
Invc.Frequency__c='Yearly';
Invc.Conversion_Rate__c=100;
Invc.Is_Auto_Invoice__c = true;
Invc.Conversion_Rate_To_USD__c=1.000000;
Invc.Type__c='Fresh';
Invc.Property_Name__c='TestMon';
Invc.From__c=system.today()-20;
Invc.To__c=system.today();
insert Invc;
System.debug('Invc-------->'+Invc);
InvoiceList.add(Invc);
AU.execute(BC, InvoiceList );
System.debug('InvoiceList-------->'+InvoiceList);
Invoice__c Invc1=new Invoice__c();
Invc1.Contract__c=Contr.id;
Invc1.Vertical__c='PS';
Invc1.InvoiceDate__c=system.today();
Invc1.Credit_Days__c=10;
Invc1.Currency__c='USD';
Invc1.Batch_Invoice_Number__c = 'abhi 01';
Invc1.Frequency__c='Half yearly';
Invc1.Conversion_Rate__c=100;
Invc1.Is_Auto_Invoice__c = true;
Invc1.Conversion_Rate_To_USD__c=1.000000;
Invc1.Type__c='Fresh';
Invc1.Property_Name__c='TestMon';
Invc1.From__c=system.today()-20;
Invc1.To__c=system.today();
insert Invc1;
System.debug('Invc-------->'+Invc);
InvoiceList.add(Invc1);
AU.execute(BC, InvoiceList );
System.debug('InvoiceList-------->'+InvoiceList);
Invoice__c Invc2=new Invoice__c();
Invc2.Contract__c=Contr.id;
Invc2.Vertical__c='PS';
Invc2.InvoiceDate__c=system.today();
Invc2.Credit_Days__c=10;
Invc2.Currency__c='USD';
Invc2.Frequency__c='Quarterly';
Invc2.Conversion_Rate__c=100;
Invc2.Conversion_Rate_To_USD__c=1.000000;
Invc2.Type__c='Fresh';
Invc2.Is_Auto_Invoice__c = true;
Invc2.Property_Name__c='TestMon';
Invc2.From__c=system.today()-20;
Invc2.To__c=system.today();
insert Invc2;
System.debug('Invc-------->'+Invc);
InvoiceList.add(Invc2);
AU.execute(BC, InvoiceList );
System.debug('InvoiceList-------->'+InvoiceList);
list<Invoice__c> i = [select id, name from Invoice__c where id = :InvoiceList];
Description__c DesObj=new Description__c();
DesObj.Invoice__c=Invc.id;
DesObj.Description__c = 'Setup fee';
DesObj.Sr_No__c=10;
Insert DesObj;
DesList.add(DesObj);
System.debug('DesList-------->'+DesList);
Description__c DesObj2=new Description__c();
DesObj2.Invoice__c=Invc2.id;
DesObj2.Description__c = 'Half yearly subscription';
DesObj2.Sr_No__c=10;
DesObj2.Amount__c=1234.00;
Insert DesObj2;
DesList.add(DesObj2);
System.debug('DesList-------->'+DesList);
Description__c DesObj1=new Description__c();
DesObj1.Invoice__c=Invc1.id;
DesObj1.Description__c = 'Half yearly subscription';
DesObj1.Sr_No__c=10;
DesObj1.Amount__c=1234.00;
Insert DesObj1;
DesList.add(DesObj1);
System.debug('DesList-------->'+DesList);
Test.startTest();
Batch_InvoiceCreation BatchInv=new Batch_InvoiceCreation ();
ID batchprocessid = Database.executeBatch(BatchInv);
Test.stopTest();
}
}
What you can do is use system.assert(as show in this sample :https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_example.htm)and compare the new and expected value to show that the code is working and the udates are happening as designed.