Last active
July 30, 2023 08:17
-
-
Save tyoshikawa1106/d8661aa24f5cf357cbda to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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