I'm pretty sure the scope in batch apex is set up automatically from its query when the execute function gets called / is run.
Here is an example Batch class with a test that covers the code 100%. Hopefully this can help you get started.
global class OpportunitiesBatch implements Database.Batchable<SObject>
{
global Database.queryLocator start(Database.BatchableContext ctx)
{
string squery = 'select Id, Name, amount from Opportunity where amount >= 100000';
if(test.IsRunningTest()==true)
{
squery = squery + ' limit 200';
}
return Database.getQueryLocator(squery);
}
global void execute(Database.BatchableContext BC,List<Sobject> scope)
{
for(Sobject s : scope)
{
Opportunity o = (Opportunity)s;
o.Name = o.Name + '-BigOpp!!!';
}
update scope;
}
global void finish(Database.BatchableContext BC)
{
//Send an email to the User after your batch completes
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'test@test.com'};
mail.setToAddresses(toAddresses);
mail.setSubject('Apex Batch Job is done');
mail.setPlainTextBody('The batch Apex job processed ');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
@isTest
private class TestOpportunities
{
private static void DataSetUp()
{
// Test Data code
Opportunity o = new Opportunity(Name = 'Test-O', Amount = 1000000, StageName = 'Prospecting', CloseDate = date.today()+5);
insert o;
}
private static testMethod void testOppBatch()
{
DataSetUp();
User u = [SELECT Id FROM User WHERE LastName = 'Test'];
System.runAs(u)
{
Test.startTest();
OpportunitiesBatch O = new OpportunitiesBatch();
Database.executeBatch(O);
Test.StopTest();
}
}
}
Hi sfdctrr,
I'm pretty sure the scope in batch apex is set up automatically from its query when the execute function gets called / is run.
Here is an example Batch class with a test that covers the code 100%. Hopefully this can help you get started.