You need to sign in to do that
Don't have an account?
tonante
ExecuteBatch() method is not invoked when testing Batch Test Class
HI , when testing batch class , may test doesn;t seem to be invoking the executeBatch() method even though it does have a valid list of opportunities for the scope. I traced the code flow in debug log to where it finished the start method in creating the QueryLocator and saw that it did contain a list of opportunities but it did not reach the executeBatch method but skipped ahead to the finish method. So now I am only able to cover 53%. I wonder why the Query Locator skipped to Finish upon return? Doesn;t it flow into executeBatch if the object for scope is not empty? Here is my class code for batch:
Thanks for your help.
global class BatchGeneratePickTicketsByQuery implements Database.Batchable<sObject> { global String Query; global Database.QueryLocator start(Database.BatchableContext BC) { String DefaultQuery = ' Select Id, AccountId, Account.Name, RecordTypeId, RecordType.Name, Name, ' + ' StageName, Amount, CreatedDate, CreatedById, CreatedBy.Name, HasOpportunityLineItem, LastModifiedDate, ' + ' LastModifiedById, LastModifiedBy.Name, TotalOpportunityQuantity, Total_Pick_Quantity__c, ' + ' Total_Pick_Tickets__c, Inventory_Items__c, Non_inventory_Items__c, Error_Log__c, ' + ' (Select Id, Name From Pick_Tickets__r) ' + ' FROM Opportunity Where CreatedDate > 2012-07-01T00:00:00.000Z ' + ' And RecordType.Name = \'Orders\' ' + ' And Amount > 0 ' + ' And (StageName = \'Order Confirmed\' OR StageName like \'%Partially%\') ' + ' And (NOT StageName like \'%Cancelled%\') ' + ' And Id Not In (Select Opportunity__c From Sales_Order__c) '; if(Query == null) { Query = DefaultQuery; } else { system.debug('<<QUERY>> '+Query); if(!Query.toLowerCase().contains('from opportunity')) { Query = null; } } system.debug('QUERY >> '+Query); return Database.getQueryLocator(Query); } global void execute(Database.BatchableContext BC, List<sObject> scope) { for(sObject s : scope) { Opportunity o = (Opportunity)s; OrderEntryStatus.QueueGeneratePickTicket(o.Id); } } global void finish(Database.BatchableContext BC) { system.debug(BC); } }2) Here is the Test Class for the batch class:
@isTest public class BatchGeneratePickTicketsByQuery_Test { static testmethod void test() { String Query = ' Select Id, AccountId, Account.Name, RecordTypeId, RecordType.Name, Name, ' + ' StageName, Amount, CreatedDate, CreatedById, CreatedBy.Name, HasOpportunityLineItem, LastModifiedDate, ' + ' LastModifiedById, LastModifiedBy.Name, TotalOpportunityQuantity, Total_Pick_Quantity__c, Company__c,Shipping_Weight__c, ' + ' Total_Pick_Tickets__c, Inventory_Items__c, Non_inventory_Items__c, Error_Log__c ' + ' FROM Opportunity Where CreatedDate > 2012-07-01T00:00:00.000Z ' + ' And RecordType.Name = \'Orders\' ' + ' And Amount >= 0 ' + ' And (Total_Pick_Tickets__c > 0 OR Pick_Ticket_Not_Fulfillable__c = true) ' + ' And StageName != \'Order Cancelled\' ' + ' And StageName != \'Order Invoiced\' ' + ' And (Not StageName like \'%Fulfilled%\') ' + ' And (StageName like \'%Partially%\' OR StageName = \'Order Confirmed\' OR Pick_Ticket_Not_Fulfillable__c = true) ' + ' And Id Not In (Select Opportunity__c From Sales_Order__c)'; Test.startTest(); BatchGeneratePickTicketsByQuery bgptbq = new BatchGeneratePickTicketsByQuery(); bgptbq.Query= null; ID batchprocessid = Database.executeBatch(bgptbq); Test.stopTest(); } }
Thanks for your help.
Just curious about the difference because to test this one successfully at 93% I just ran this: