1

Please help me to finish my Test Class for the following Batch Job generating Invoice creation.

Apex Batch Job

global class InvoiceGenerateBatchJob implements Database.Batchable<SObject> {

    global Database.QueryLocator start(Database.BatchableContext BC) {


        return Database.getQueryLocator([
                SELECT Name, Id, Rent_Due_Date__c, Monthly_Rate__c, Property__c
                FROM Tenant__c
                WHERE Rent_Due_Date__c = TODAY
        ]);
    }

    global void execute(Database.BatchableContext BC, List<Tenant__c> tenants) {

        List<Invoice__c> invoices = new List<Invoice__c>();

        for (Tenant__c tenant : tenants) {
            for(Invoice__c invoice : invoices){

                invoice.Bill_To_Tenant__c = tenant.Name;
                invoice.Due_Date__c = tenant.Rent_Due_Date__c;
                invoices.add(invoice);
            }
        }
        insert invoices;
    }

    global void finish(Database.BatchableContext BC) {

        AsyncApexJob job = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed,TotalJobItems, CreatedBy.Email
                            FROM AsyncApexJob
                            WHERE Id = :BC.getJobId()];
    }

}

Scheduler Class For Batch Apex

global class InvoiceGenerateBatchScheduled implements Schedulable {
    global void execute(SchedulableContext sc) {
        InvoiceGenerateBatchJob invBatch = new InvoiceGenerateBatchJob();
        Database.executeBatch(invBatch, 200);

    }
}

​​​​​​Testing Batch Apex Job and Scheduler Classes:

@IsTest
private class InvoiceGenerateBatchTest {

    @TestSetup
    static void setupData() {

        List<Property__c> prop = new List<Property__c>();
        Property__c property = new Property__c();
        property.Name = 'Prop1';
        property.Business__c = 'Gov';
        prop.add(property);
        insert prop;

        List<Tenant__c> tenants = new List<Tenant__c>();
        for (Integer i = 1; i < 5; i++);{

            try {
                Tenant__c t = new Tenant__c();
                t.Name = 'TenOne';
                t.Property__c = [
                        SELECT Id
                        FROM Property__c
                        WHERE Name = 'Prop1'
                        LIMIT 5
                ].Id;
                tenants.add(t);
            } catch (Exception e) {

                insert tenants;
            }
        }
    }

    @IsTest
     static void testInvoiceGenerateBatch() {

     String CRON_EXP = '0 0 22 30 3 ?';
     Date dueDate = Date.today();

     Test.startTest();

     String jobId = System.schedule('CreateInvoice', CRON_EXP, new InvoiceGenerateBatchScheduled());
     Database.executeBatch(new InvoiceGenerateBatchJob());

     Test.stopTest();

     System.assertEquals(5, [SELECT COUNT() FROM Invoice__c WHERE Bill_To_Tenant__r.Name = 'TenOne']);

        }
    }
1
0

In your start Method filter is Rent_Due_Date__c = TODAY

In test class,You need to provide the value for Rent Due date

    Tenant__c tenant = new Tenant__c();
    tenant.Name = 'TenOne';
    tenant.Rent_Due_Date__c = System.today();
    tenant.Property__c = property.Id;
    ten.add(tenant);
    insert ten;
 //Create Invoice records as well

If start method doesn't have any records,then execute method will not work

6
  • Thank you Annappa for your input. The Rent_due_Date is on Tenant object, so I have made the corrections to my code. But it is still covering only 76% and I'm expecting that 1 invoice will be created, but it shows 0. Mar 31 '20 at 5:17
  • yes thats on tenant object. i have updated my answer. if it resolves your query make it as best answer and close the question Mar 31 '20 at 5:24
  • So I've created Invoice records as well and technically the test has passed, but still shows only 76% of coverage. How can I make it 100%? Mar 31 '20 at 5:40
  • Please update new question. dont modify the original question Mar 31 '20 at 5:53
  • 1
    Please read our canonical resource on test coverage.
    – David Reed
    Mar 31 '20 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.