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:
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:
Thanks for your help.@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();
}
}
Please try below test class
@isTest()
public class BatchGeneratePickTicketsTest
{
static testmethod void test()
{
Account acc= new Account(Name = 'testAcc',BillingStreet='testStreet',BillingCity ='tectcity',BillingState='testState',BillingPostalCode='123',
BillingCountry='testcountry',Description='testdesc');
insert acc;
//Case record in Test method.
Contact conObj = new Contact();
conObj.lastname = 'testcon';
conObj.AccountId = acc.id;
insert conObj;
List<Opportunity> lstOpp = new List<Opportunity>();
Opportunity opp= new Opportunity(AccountId=acc.id,Amount=1234.00,Description='testdesc',Name='testOpp',
StageName='Prospecting',CloseDate = System.Today());
insert opp;
lstOpp.add(opp);
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 ' ;
Test.startTest();
BatchGeneratePickTickets bgptbq = new BatchGeneratePickTickets();
bgptbq.Query= Query;
bgptbq.OrdersToPick = lstOpp;
ID batchprocessid = Database.executeBatch(bgptbq);
Test.stopTest();
}
}
Please let us know if this will hep u