You need to sign in to do that
Don't have an account?
aam1r
Query returning no records in test after execution of Batch job
I'm hoping someone can help. I have a batch job that is run via a button click in a Lightning page. the fucnitonality works fine but i'm struggling with the test class. After i call the Batch apex method to test, i'm getting full code coverage but any asserts afetr the run are not returning records as i'd expect. In fact, it's not returning any records.
Here is the test code extract that is executing the batch class:
A debug on the batch class post insert:
Can someone help me understand what i could be doing wrong please?
Many thanks for your time and apologies if my question was not asked in the best of ways.
regards,
Aamir
Here is the test code extract that is executing the batch class:
@isTest public static void test_FirstHeaderGeneratorBatch_2(){ String OppToTest = 'Opportunity 1'; Opportunity opp = [SELECT Id, First_Header_Required__c, First_Header_Created__c, AccountId FROM Opportunity WHERE Name =:OppToTest LIMIT 1]; System.assert(opp.First_Header_Created__c==false, OppToTest+' should not have processed invoicing'); // succeeds Integer headersBefore = [SELECT Id FROM Header__c WHERE Opportunity__c = :opp.Id].size(); System.assert(headersBefore == 0, OppToTest+' should not have generated headers'); // succeeds List<OpportunityLineItem> olis = [SELECT Id, Ordered_Date__c, First_Payment_Date__c, Supplier__r.Name FROM OpportunityLineItem WHERE OpportunityId =: opp.Id]; for (OpportunityLineItem oli : olis) { System.assert(oli.Ordered_Date__c==null, OppToTest+' should not have ordered products'); } for (OpportunityLineItem oli : olis) { if (oli.Supplier__r.Name=='InternalSupplier'){ oli.Ordered_Date__c = System.today(); oli.First_Payment_Date__c = System.today().addMonths(1); } } update olis; Test.startTest(); //////////////////////////////////////// FirstHeaderGeneratorBatch.executeBatch(Date.today()); Test.stopTest(); ///////////////////////////////////////// Integer headersAfter = [SELECT Id FROM Header__c WHERE Opportunity__c = :opp.Id].size(); /////// ASSERT FAILS System.assert(headersAfter > 0, 'Additional header(s) should have been created. Before: '+headersBefore+', After: '+headersAfter); }The batch does execute and teh logs are showing teh whole process, however the last assert is returning zero. The query (at line 31)
Integer headersAfter = [SELECT Id FROM Header__c WHERE Opportunity__c = :opp.Id].size();.. returns zero records, although i can see the inserts succeeding as part of the batch class execution.
A debug on the batch class post insert:
insert headersToInsert; System.debug(' ^^^ LineItems inserted: '+[SELECT Id FROM Header__c].size());..returns as expected in the logs:
Can someone help me understand what i could be doing wrong please?
Many thanks for your time and apologies if my question was not asked in the best of ways.
regards,
Aamir
could you please share your batch class and the test class completly?
regards,
Omar
Hi Omar - and thanks for getting back to me. Please find below an screened and slighytly trimmed version of the code, which contains all the essentials required. I've only removed lengthy calculations and processes that will only clog-up the page. Also i had to rename some of the fields and objects so as to remove any reference to the client. Bearing that in mind this code is untested.
Batch Class: