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();
        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');

            if(ac.Tech_IsWrong_Acct__c == true && ac.AL_DUNS_Number_compagny__c == null){
            } else {

        upsert scope;

    global void finish(Database.BatchableContext BC) {

1 Answer 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.

private class BatchUpdateAccountTest {

    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;

    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();
        Batch_Update_account batchJob = new Batch_Update_account(accountIds);
        // 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:

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

You must log in to answer this question.

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