Skip to content

Instantly share code, notes, and snippets.

@tyoshikawa1106
Last active July 30, 2023 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tyoshikawa1106/d8661aa24f5cf357cbda to your computer and use it in GitHub Desktop.
Save tyoshikawa1106/d8661aa24f5cf357cbda to your computer and use it in GitHub Desktop.
public class LeadProcessor implements Database.Batchable<sObject>, Database.Stateful {
/**
* コンストラクタ
*/
public LeadProcessor() {
}
/**
* Start
*/
public Database.QueryLocator start(Database.BatchableContext BC) {
String query = 'SELECT Id FROM Lead';
return Database.getQueryLocator (query);
}
/**
* Execute
*/
public void execute(Database.BatchableContext BC, List<Lead> leads) {
for (Lead l : leads) {
l.LeadSource = 'Dreamforce';
}
update leads;
}
/**
* Finish
*/
public void finish(Database.BatchableContext BC) {
}
}
@isTest
private class LeadProcessorTest {
private static User testAdminUser = new User(Id = UserInfo.getUserId());
static testMethod void LeadProcessorTest() {
System.runAs(testAdminUser) {
List<Lead> leads = new List<Lead>();
for (Integer i = 0; i < 200; i++) {
leads.add(new Lead(LastName = 'Yoshikawa', Company = 'T.Yoshikawa Labs'));
}
insert leads;
System.assertEquals(leads.size(), 200);
Test.startTest();
LeadProcessor batchable = new LeadProcessor();
Database.executeBatch(batchable);
Test.stopTest();
List<Lead> results = [SELECT Id,LeadSource FROM Lead];
for (Lead l : results) {
System.assertEquals(l.LeadSource, 'Dreamforce');
}
System.assertEquals(results.size(), 200);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment