2

Need to raise the covered line on this apex code could you kindly point out the missing piece of code for my test class. It does not covered the code in bold face (global void execute(Database.BatchableContext BC...) (between ** and **).

APEX CLASS

global class DefaultGracePeriod implements Database.Batchable<sObject>{

    global Database.QueryLocator start(Database.BatchableContext BC){
    Date sixWeeksBefore = Date.Today().addDays(30);

        String query = 'SELECT Id, Name, SubscriptionEndDate__c, Grace_Period__c, Billing_Type__c, TermSettingType__c '+
                       'FROM  Subscription__c WHERE SubscriptionEndDate__c = :sixWeeksBefore AND Billing_Type__c = \'Full Prepaid\' '+
                       'AND TermSettingType__c = \'TERMED\'';
        return Database.getQueryLocator(query);
    }

    **global void execute(Database.BatchableContext BC, List<Subscription__c> scope){    
    List<Subscription__c> eligibleSubsForGrace = new List<Subscription__c>();

    for(Subscription__c zGraceSub : scope) {
        zGraceSub.Grace_Period__c = 14;
        eligibleSubsForGrace.add(zGraceSub);
    }
    update eligibleSubsForGrace;
    }**

    global void finish(Database.BatchableContext BC){
    }
}

TEST CLASS

@isTest(seeAllData = TRUE)

public class DefaultGracePeriodTest {

    static testmethod void test() {

        //create project
        Project__c pro = TestData.createProject(1)[0];
        pro.Project_Description__c = 'This is a new confidential project.';
        //create vendor subscriber acct
        Account vendorSubsAcct = TestData.createAccount(1, 'Vendor Subscriber')[0];
        //create singpost addresscreateAccount
        Singpost_Address__c singpostAdd = TestData.createSingpostAddress(1)[0];
        //create subscription vendor
        Subscription_Vendor__c subsVen = TestData.createSubscriptionVendor(1)[0];
        //create package
        Package__c pack = TestData.createPackage(1)[0];
        //create vendor Account
        Account venAccount = TestData.createAccount(1, 'Vendor')[0];
        insert new List<sObject>{pro, vendorSubsAcct, singpostAdd, subsVen, pack, venAccount};

        zqu__ZProduct__c zProduct = TestData.createZProduct(1)[0];
        insert zProduct;

        zqu__ProductRatePlan__c prdRatePlan = TestData.createProductRatePlan(1)[0];
        prdRatePlan.zqu__ZProduct__c = zProduct.Id;
        insert prdRatePlan;

        zqu__ProductRatePlanCharge__c prpc = TestData.createProductRatePlanCharge(1)[0];
        prpc.zqu__ProductRatePlan__c = prdRatePlan.Id;
        insert prpc;

        zqu__ProductRatePlanChargeTier__c prpct = TestData.createProductRatePlanChargeTier(1)[0];
        prpct.zqu__ProductRatePlanCharge__c = prpc.Id;
        prpct.zqu__PriceFormat__c = 'Per Unit';
        prpct.zqu__Currency__c ='SGD';
        insert prpct;

        Package_Item__c pItem = TestData.createPackageItem(1, 'Base Product')[0];
        pItem.Package__c = pack.Id;
        pItem.Product_Rate_Plan__c = prdRatePlan.Id;
        insert pItem;

        //create address
        Address__c add = TestData.createAddress(1)[0];
        add.Postal_Code__c = singpostAdd.Id;
        insert add;

        //create billing acct
        Zuora__CustomerAccount__c billingAcc = TestData.createBillingAccount(1)[0];
        billingAcc.Zuora__Account__c = vendorSubsAcct.Id;
        insert billingAcc;

        //create contact
        Contact con = TestData.createContact(1)[0];
        con.AccountId = vendorSubsAcct.Id;
        con.Address__c = add.Id;
        insert con;

        //create order
        Order__c order = TestData.createOrder(1)[0];
        order.Account__c = vendorSubsAcct.Id;
        order.Recipient_Contact__c = con.Id;
        order.Billing_Account__c = billingAcc.Id;
        order.Informed_Customer__c = True;
        insert order;

        //create case
        Case cas = TestData.createCase(1, 'Complaint - Magazine')[0];
        cas.Order__c = order.Id;
        insert cas;

        //create order line item
        Order_Line_Item__c oli = TestData.createOrderLineItem(1)[0];
        oli.Order__c = order.Id;
        oli.Vendor_Subscription_ID__c = subsVen.Id;
        oli.Main_Package_ID__c = pack.Id;
        oli.Vendor_Mailing_Agent__c = vendorSubsAcct.Id;
        insert oli;

        Gift__c gift = TestData.createGift(1)[0];
        gift.Gift_Type__c = 'Premium';
        insert gift;

        Promotion_Gift__c pg = TestData.createPromotionGift(1)[0];
        pg.Gift__c = gift.Id;
        insert pg;

        Order_Line_Item_Gift__c olig = TestData.createItemGift(1)[0];
        olig.Order_Line_Item__c = oli.Id;
        olig.Promotion_Gift__c = pg.Id;
        insert olig;

        //create Zuora Subscriptions
        Subscription__c zSub = TestData.createSubscription(1)[0];
        zSub.Zuora__Account__c = vendorSubsAcct.Id;
        zSub.Order_Line_Item_Number__c = oli.Id;
        zSub.Order_Number__c = order.Id;
        zSub.Grace_Period__c = 14;
        zSub.Suppress_Vendor_Start_Notice__c = 'false';
        zSub.Supressed_Acknowledgement_Letter__c = 'false';
        zSub.Supressed_Renewal_and_Reminder_Letter__c = 'false';
        zSub.Urgent__c = 'false';
        zSub.Vendor_Conversion__c = 'false';
        zSub.Charity__c = 'false';
        insert zSub;

        //create parcel
        Parcel__c par = TestData.createParcel(1)[0];
        par.Project_Name__c = pro.Id;
        par.Allocation_Type__c = 'SDA';
        par.Description__c = 'Description';
        insert par;

        //create vendor allocation
        Vendor_Allocation__c venAlloc = TestData.createVendorAlloc(1)[0];
        venAlloc.SingPost_Address__c = singpostAdd.Id;
        venAlloc.Vendor_Account__c = venAccount.Id;
        venAlloc.Parcel_Name__c = par.Id;
        insert venAlloc;



       Test.startTest();
       DefaultGracePeriod batch = new DefaultGracePeriod();
       ID batchprocessid = Database.executeBatch(batch, 150);
       Test.stopTest();
    }
}
5

The execute method of your Batchable is only called if the query in the start method returns records. That query includes this where clause:

SubscriptionEndDate__c = :sixWeeksBefore
AND Billing_Type__c = \'Full Prepaid\' '+
'AND TermSettingType__c = \'TERMED\'';

so (assuming other logic doesn't set these fields) your unit test will have to set matching values:

zSub.SubscriptionEndDate__c = Date.Today().addDays(30);
zSub.Billing_Type__c = 'Full Prepaid';
zSub.TermSettingType__c = 'TERMED';

so that the query returns a record and the execute gets called for that record.

1

The code enters the execute method only if the SOQL Query in the START Method returns some records. So make sure your test method has the data that matches.

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.