I have managed to put together a batch test class using Trailhead, but it is not correct, all I need is for it to populate values in new custom fields with values in other fields all from the Account object.
Batch class
global class MigrateCreditSafe implements Database.Batchable<SObject> {
public static void runBatch() {
MigrateCreditSafe batch = new MigrateCreditSafe();
Database.executeBatch(batch);
}
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator(
[SELECT bluetahiti__creditSafeScore__c, bluetahiti__creditSafeCompanyType__c, bluetahiti__creditSafeScoreDescription__c,
bluetahiti__creditSafeLastRefreshed__c, bluetahiti__creditSafeLimit__c
FROM Account WHERE bottomline__creditSafeScore__c != null]
);
}
global void execute(Database.BatchableContext bc, List<Account> scope) {
for(Account acc : scope){
acc.bottomline__creditSafeScore__c = acc.bluetahiti__creditSafeScore__c;
acc.bottomline__creditSafeCompanyType__c = acc.bluetahiti__creditSafeCompanyType__c;
acc.bottomline__creditSafeScoreDescription__c = acc.bluetahiti__creditSafeScoreDescription__c;
acc.bottomline__creditSafeLastRefreshed__c = acc.bluetahiti__creditSafeLastRefreshed__c;
acc.bottomline__creditSafeLimit__c = acc.bluetahiti__creditSafeLimit__c;
}
update scope;
}
global void finish(Database.BatchableContext bc) {}
}
Test class
@isTest
public class MigrateCreditSafeTest {
@testSetup
static void createAccount() {
List<Account> accounts = new List<Account>();
// insert some records
for (Integer i=0; i<10; i++) {
accounts.add(new Account(name='Account '+i, billingcountry='United Kingdom', bottomline__creditSafeScore__c=50, bluetahiti__creditSafeScoreDescription__c='High Risk'));
}
insert accounts;
Test.startTest();
MigrateCreditSafe.runBatch();
Test.stopTest();
}
static testmethod void test() {
// after testing stops, assert records were updated
System.assertEquals(50, account.bluetahiti__creditSafeScore__c=50);
System.assertEquals('High Risk', account.bluetahiti__creditSafeScoreDescription__c='High Risk');
}