1

I'm struggling with an Test Class for a little batch I created, let me clarify that I'm not a developer, so any tips will be appreciated.

I wrote 2 versions of the class, the first gives me about 94% coverage, but I was not creating products and pricebook entries in the class, so I changed it to add those and the code coverage went down to 83%. The test passes everytime, but the system assert fails.

Here's my class

global class batchCalculateMRR implements Database.Batchable<sObject>{
global Database.QueryLocator start(Database.BatchableContext BC){

    String query = 'SELECT Id, (SELECT Id, Vigente__c, Es_Recurrente__c, TotalPrice, OpportunityId FROM OpportunityLineItems) FROM Opportunity WHERE HasOpportunityLineItem = TRUE';
    return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC, List<Opportunity> oppsWithProducts)
{

List<Opportunity> oppsToUpdate = new List<Opportunity>();
    for (Opportunity oppWithProduct : oppsWithProducts) {
        System.debug('Opp Id: '+ oppWithProduct.Id);

        //List<Double> toSum = new List<Double>();
        Double MRRSum = 0.0;

        for (OpportunityLineItem oppLineItem : oppWithProduct.OpportunityLineItems) {
            System.debug('Opp Line Item: ' + oppLineItem.Id);

            if (oppLineItem.Vigente__c == TRUE && oppLineItem.Es_Recurrente__c == TRUE) {
            //toSum.add(oppLineItem.TotalPrice);
            System.debug('To Sum: ' + oppLineItem.TotalPrice);
            MRRSum = MRRSum + oppLineItem.TotalPrice;
            } else if (oppLineItem.Vigente__c == FALSE && oppLineItem.Es_Recurrente__c == TRUE 
                        || oppLineItem.Vigente__c == TRUE && oppLineItem.Es_Recurrente__c == FALSE
                        || oppLineItem.Vigente__c == FALSE && oppLineItem.Es_Recurrente__c == FALSE) {
                    //toSum.add(0.0);
                    System.debug('To Sum: ' + 0.0);
                    MRRSum = MRRSum + 0.0;
            }
        }
        //System.debug('Total Price to Sum: '+ toSum.size());
        //System.debug(toSum);
        System.debug('Final MRRSum: ' + MRRSum);

        //Create List to Update Opps
        oppWithProduct.MRR_Test_JB__c = MRRSum;
        oppsToUpdate.add(oppWithProduct);
        //System.debug('MRR Test JB'+ oppWithProduct.MRR_Test_JB__c);
        }
                Update oppsToUpdate;
}  


global void finish(Database.BatchableContext BC){
}

And the test class I'm currently working on:

    //See PriceBook
    Id priceBookId = Test.getStandardPricebookId();

    //Create your products
    Product2 firstProduct = new Product2(
        Name = 'First Product',
        ProductCode = 'Pro-1',
        isActive = true,
        CurrencyIsoCode = 'USD'
        );
        insert firstProduct;

    Product2 secondProduct = new Product2(
        Name = 'Second Product',
        ProductCode = 'Pro-2',
        isActive = true,
        CurrencyIsoCode = 'USD'
        );
        insert secondProduct;

    Product2 thirdProduct = new Product2(
        Name = 'Third Product',
        ProductCode = 'Pro-3',
        isActive = true,
        CurrencyIsoCode = 'USD'
        );
        insert thirdProduct;


    //Create your pricebook entries      
    PricebookEntry firstPricebookEntry = new PricebookEntry(
        Pricebook2Id = pricebookId,
        Product2Id = firstProduct.Id,
        UnitPrice = 10.00,
        IsActive = true,
        CurrencyIsoCode = 'USD'
        );
        insert firstPricebookEntry;

    PricebookEntry secondPricebookEntry = new PricebookEntry(
        Pricebook2Id = pricebookId,
        Product2Id = secondProduct.Id,
        UnitPrice = 10.00,
        IsActive = true,
        CurrencyIsoCode = 'USD'
        );
        insert secondPricebookEntry;

    PricebookEntry thirdPricebookEntry = new PricebookEntry(
        Pricebook2Id = pricebookId,
        Product2Id = thirdProduct.Id,
        UnitPrice = 10.00,
        IsActive = true,
        CurrencyIsoCode = 'USD'
        );
        insert thirdPricebookEntry;

    //Create Opportunities
    List<Opportunity> newOpps = new List<Opportunity>();
    for(Integer i=0 ;i <100;i++) {
        Opportunity opp = new Opportunity();
        opp.Name ='Name'+i;
        opp.StageName = '1.Contactando al decisor';
        opp.Pricebook2Id = priceBookId;
        opp.CloseDate =  Date.today();
        opp.CurrencyIsoCode = 'USD';
        newOpps.add(opp);
    }
    insert newOpps;

    //Create Opportunity Line Items
    List<OpportunityLineItem> newOlis = new List<OpportunityLineItem>();
    for (Opportunity newOpp : newOpps) {

        OpportunityLineItem oli = new OpportunityLineItem();
        oli.OpportunityId = newOpp.Id;
        oli.TotalPrice = firstPricebookEntry.UnitPrice;
        //oli.Product2Id = firstProduct.Id;
        oli.PricebookEntryId = firstPricebookEntry.Id;
        oli.End_Date__c = Date.today().addDays(+1);
        oli.Es_Recurrente__c = True;
        oli.Quantity = 1;
        oli.Es_upselling__c = 'False';
        newOlis.add(oli);

        OpportunityLineItem oli2 = new OpportunityLineItem();
        oli2.OpportunityId = newOpp.Id;
        oli2.TotalPrice = secondPricebookEntry.UnitPrice;
        //oli2.Product2Id = secondProduct.Id;
        oli2.PricebookEntryId = secondPricebookEntry.Id;
        oli2.End_Date__c = Date.today().addDays(-5);
        oli2.Es_Recurrente__c = True;
        oli2.Quantity = 1;
        oli2.Es_upselling__c = 'False';
        newOlis.add(oli2);

        OpportunityLineItem oli3 = new OpportunityLineItem();
        oli3.OpportunityId = newOpp.Id;
        oli3.TotalPrice = thirdPricebookEntry.UnitPrice;
        //oli3.Product2Id = thirdProduct.Id;
        oli3.PricebookEntryId= thirdPricebookEntry.Id;
        oli3.End_Date__c = Date.today();
        oli3.Es_Recurrente__c = True;
        oli3.Quantity = 1;
        oli3.Es_upselling__c = 'False';
        newOlis.add(oli3);
    }
    insert newOlis;

    Test.startTest();

        batchCalculateMRR batchCalculateMRR_Test= new batchCalculateMRR();
        DataBase.executeBatch(batchCalculateMRR_Test); 

    Test.stopTest();

    //Validate Opportunity MRR_Test_JB__c
    List<Opportunity> oppsAssert = [SELECT Id, MRR_Test_JB__c FROM Opportunity];
    for (Opportunity oppAssert : oppsAssert) {
        System.debug(oppAssert.MRR_Test_JB__c);
        System.assertEquals(20.0, oppAssert.MRR_Test_JB__c);
    }
}

The problem is, in the debug logs I see all 100 Opps each with 3 OLIs but the field MRR_Test_JB__c (which should be updated in the actual batch class) is empty so my System.assertEquals fails.

Can anyone help me with this?

Thank you!!

  • 1
    What's your batch class do? In particular, what query does it run in start()? – David Reed Jul 17 '19 at 19:24
  • @DavidReed I added the batch class to the question. – Jesica Biecher Jul 17 '19 at 19:35
  • N.B. you can simply do an update oppsWithProduct instead of creating a new list at the end of execute() – cropredy Jul 17 '19 at 20:35
1

In your code, the field MRR_Test_JB__c only updates with the TotalPrice if:

oppLineItem.Vigente__c == TRUE && oppLineItem.Es_Recurrente__c == TRUE

But in your test, you are only setting the OpportunityLineItems' Es_Recurrente__c field to true, and not Vigente__c so the batch is always adding 0.0 instead of the TotalPrice

I'm not sure why you need the "else if" logic (i.e., if Vigente__c or Es_Recurrente__c aren't true) at all, since adding 0.0 doesn't affect the MRR_Test_JB__c amount.

|improve this answer|||||
  • Vigente__c is actually a formula field which checks the ServiceDate and End_Date__c. I was giving it a date for End_Date__c but not for ServiceDate so nothing was happening! I'll try removing the else if part! I'm new to this, so I'm still learning! Thank you!! – Jesica Biecher Jul 17 '19 at 20:33
  • I removed the else if and I got 100% coverage! Thanks again! – Jesica Biecher Jul 17 '19 at 22:09

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.