0

I stuck into a test class while covering try-catch block. Any help will be welcomed. unable to cover try-catch.

Here is a batch class-

global class GetXeroApprovedInvoice implements Database.Batchable<sObject>,Database.AllowsCallouts{

  global Database.QueryLocator start(Database.BatchableContext BC){
    string query = 'SELECT ID,Xero_InvoiceID__c,Status__c,Accounting_Org_Id__c FROM Invoice__c WHERE  Xero_InvoiceID__c !=NULL AND Accounting_Org_Id__c != NUll';//ID =: invoiceIds AND
    return Database.getQueryLocator(query);
  }

  global void execute(Database.BatchableContext BC, List<Invoice__c> scope){
    System.debug('scope>>>>'+scope);
    for(Invoice__c s : scope){
        try{
            ApprovedInvoices.getApprovedInvoices(s);
        }catch(Exception e){
            e.getMessage();
            System.debug('scope>>>>'+e.getMessage());
            System.debug('scope>>>>'+e.getLineNumber());
        }
    }
  }

  global void finish(Database.BatchableContext BC){

  }
}

test class-

@isTest
public class GetXeroApprovedInvoiceTest {
 Public static testMethod void totestBatchXeroSync()
 {
    Database.QueryLocator QL;
    Database.BatchableContext BC;

    set<id> ids = new set<id>();
    List<Invoice__c> invoiceList = new List<Invoice__c>();
    GetXeroApprovedInvoice objBatch = new GetXeroApprovedInvoice();
    ApprovedInvoices objApproved = new ApprovedInvoices();
    for(Invoice__c obj : invoiceList){
    ApprovedInvoices.getApprovedInvoices(obj);
    }

    QL = objBatch.start(bc);
    Database.QueryLocatorIterator QIT =  QL.iterator();
    while(QIT.hasNext())
    {
        Invoice__c Inc = (Invoice__c)QIT.next();            
        System.debug(Inc);
        invoiceList.add(Inc);               
    }        
    objBatch.execute(BC, invoiceList);
    objBatch.finish(BC);     
 }      
}
4
  • 1
    You will have to set it up to throw an error in ApprovedInvoices. Since you do not show that code it is had to say how
    – Eric
    Mar 10, 2017 at 7:45
  • Also don't forget to add asserts that confirm the logic works - that is usually more important than covering every line of code.
    – Keith C
    Mar 10, 2017 at 8:27
  • unless you are doing callouts within the batch execute(), you don't need to test start(), execute(), finish() separately. Simply mock Invoices and use @Viviana answer. Also global is unnecessary - use public
    – cropredy
    Mar 11, 2017 at 7:35
  • Thank you cropredy..... your idea worked. Thanks for the key points. Mar 14, 2017 at 6:10

1 Answer 1

1

I think you have to add Test.startTest(); and Test.stopTest(); like that:

/* first
insert data that your batch need, in particular that your query need
*/

Test.startTest();
 GetXeroApprovedInvoice batch = new GetXeroApprovedInvoice();
 Database.executeBatch(batch);
Test.stopTest();

And I'd structure the query in the batch like this:

global GetXeroApprovedInvoice()
{
    query= 'your query';
}
global Database.QueryLocator start(Database.BatchableContext BC)
{
    return Database.getQueryLocator(query);
}

where query is

Global final String query;

Instead of:

global Database.QueryLocator start(Database.BatchableContext BC){
string query = 'SELECT ID,Xero_InvoiceID__c,Status__c,Accounting_Org_Id__c FROM Invoice__c WHERE  Xero_InvoiceID__c !=NULL AND Accounting_Org_Id__c != NUll';//ID =: invoiceIds AND
return Database.getQueryLocator(query);

}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .