LeadProcessorTest.apex
@isTest
public class LeadProcessorTest {
@isTest
public static void testit() {
List<Lead> leads = new List<Lead>();
// insert 200 Lead records
for (Integer i=0;i<200;i++) {
Lead l = new Lead();
l.LastName = 'name' + i;
l.Company = 'Company';
l.Status = 'Random Status';
leads.add(l);
}
insert leads;
//이 사이에서 해야한다
Test.startTest();
LeadProcessor lp = new LeadProcessor();
Id batchId = Database.executeBatch(lp);
Test.stopTest();
}
}
LeadProcessor.apex
global class LeadProcessor implements Database.Batchable<sObject>{
//stateful은 필요없으니 삭제
Integer count = 0;
//start
global Database.QueryLocator start(Database.BatchableContext bc) {
//collect all Lead update
return Database.getQueryLocator('SELECT ID, LeadSource FROM Lead');
}
global void execute(Database.BatchableContext bc, List<Lead> scope){
LIST<Lead> leads = new LIST<Lead>();
for (Lead lead : scope) {
lead.LeadSource = 'Dreamforce';
leads.add(lead);
count += 1;
}
update leads;
}
global void finish(Database.BatchableContext bc){
System.debug('count = ' + count);
}
}