0

i want to make test with this class, but i'm new with salesforce / Apex and i don't know what i do for it.

global class Batch_Update_account implements Database.Batchable<sObject>,Database.AllowsCallouts, Database.Stateful {
    
    global Set<Id> ids;
    
    global Batch_Update_account(Set<Id> accountIds) {
        ids = accountIds;
    }
    
    global Database.QueryLocator start(Database.BatchableContext BC) {

        String query = 'SELECT Id, N_Fiscal__c, AL_DUNS_Number_compagny__c FROM ACCOUNT WHERE Id IN :ids';
        return Database.getQueryLocator(query);

    }

    global void execute(Database.BatchableContext BC, List<Account> scope) {
        /*
        datetime start = system.Now();
        while(System.Now()<start.addSeconds(1)){} 
        */
        System.debug('execute Batch_Update_account');
        DNB_Token token = new DNB_Token();
        Account[] toDelete = new Account[]{};
        Account[] toUpsert = new Account[]{};
        for(Account ac : scope){
            System.debug('Call of populateDuns');
            AccountAction.populateDuns(ac,token);

            /*
            if(ac.Tech_IsWrong_Acct__c == true && ac.AL_DUNS_Number_compagny__c == null){
                toDelete.add(ac);
            } else {
                toUpsert.add(ac);
            }*/
        }

        //AccountAction.handleToDeleteAccount(toDelete);
        
        upsert scope;
        token.save();
    }

    global void finish(Database.BatchableContext BC) {
       
    }
}

1 Answer 1

1

The first thing you need to do is create a new apex class, and add the @IsTest annotation at the top of it, then I would strongly suggest to create a method and add this annotation to it: @TestSetup, in that method you create all test data that your process will use. And third, add as many test methods as needed (to make sure all use cases are covered) test methods are annotated with @IsTest annotation as well.

@IsTest
private class BatchUpdateAccountTest {

    @TestSetup
    static void initData() {
        // add all required fields
        Account appelTest = new Account(
            Name = 'Appel Inc',
            N_Fiscal__c = 2011, 
            AL_DUNS_Number_compagny__c = 'TBD'
        );
        Account sonyTest = new Account(
            Name = 'Sony Corp',
            N_Fiscal__c = 1984, 
            AL_DUNS_Number_compagny__c = 'TBD'
        );
        // records inserted in this method are available in ALL @IsTest methods
        List<Account> accounts = new List<Account>{appelTest, sonyTest};
        insert accounts;
    }

    @IsTest
    static void testBatchPositiveScenario() {
        // this will return 2 Ids (the ones created on @TestSetup method)
        Set<Id> accountIds = (new Map<Id, Account>([SELECT Id FROM Account])).keySet();
        Test.startTest();
        Batch_Update_account batchJob = new Batch_Update_account(accountIds);
        Database.executeBatch(batchJob);
        Test.stopTest();
        // add meaningful assertion to make sure the batch job did his job as per the requirements
    }
}

You can use the above class as a starting point, but you get the general idea. useful links:

1
  • Thanks for the answer, I think I have all the information I need to conduct another test.
    – DiutG
    May 20 at 8:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.