0

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');
    }
0

1 Answer 1

4

A testmethod can't reference variables from @testSetup or see the results of updates done there so you have to requery the Accounts in the testmethod:

static testmethod void test() {

    Account[] accounts = [
            select bluetahiti__creditSafeScore__c, bluetahiti__creditSafeScoreDescription__c
            from Account
            ];
    System.assertEquals(10, accounts.size());

    for (Account a : accounts) {   
        System.assertEquals(50, a.bluetahiti__creditSafeScore__c);
        System.assertEquals('High Risk', a.bluetahiti__creditSafeScoreDescription__c);
        ...
    }
}

As your batchable updates fields like bottomline__creditSafeScore__c you should be asserting those values.

The syntax of your asserts is quite odd in the question as you are attempting a value assignment there too. Also I would expect the compiler to interpret e.g. account.bluetahiti__creditSafeScore__c as an SObjectField token not a reference to a value and so would expect compilation errors.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .