Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Salesforce by (11.9k points)

I am trying my hands on implementing a batch process. I need some help/guidance on how to test this. All I am doing here is to display the opportunity name in the debug logs. But when I run the class scheduledBatchable which also has the test class in it in Apex test execution. The debug statements on the Opp_BatchProcess are not getting displayed. What is that I am doing wrong?

Here is the code I have

global class Opp_BatchProcess implements Database.Batchable < sObject >

{

    globalDatabase.QueryLocator start(Database.BatchableContextBC)

    {

        system.debug('Insidestart');

        returnDatabase.getQueryLocator('select name,id from opportunity');

    }

    global void execute(Database.BatchableContext BC, List <sObject> batch)

    {

        for (Sobject s : batch)

        {

            opportunity o = (opportunity)s;

            system.debug('Opp name is' + o.name);

        }

    }

    global void finish(Database.BatchableContext BC) {}

}

I also have a schedulable class

global class scheduledBatchable implements Schedulable

{

    global void execute(SchedulableContext sc)

    {

        Opp_BatchProcess b = new Opp_BatchProcess();

        ID myBatchJobID = database.executebatch(b);

    }

    public static testMethod void testscheduleMerge()

    {

        Test.startTest();

        scheduledBatchable s8 = new scheduledBatchable();

        string sch = '0 0 * * 1-12 ? *';

        system.schedule('Process Trans 1', sch, s8);

        Test.stopTest();

    }

}

1 Answer

0 votes
by (32.1k points)

It seems your testmethod only tests the Scheduled class.

You need to actually test the Batchable class as well.

Try this:

global class scheduledBatchable implements Schedulable

{

    global void execute(SchedulableContext sc)

    {

        Opp_BatchProcess b = new Opp_BatchProcess();

        ID myBatchJobID = database.executebatch(b);

    }

    public static testMethod void testscheduleMerge()

    {

        Test.startTest();

        scheduledBatchable s8 = new scheduledBatchable();

        string sch = '0 0 * * 1-12 ? *';

        system.schedule('Process Trans 1', sch, s8);

        Test.stopTest();

    }

    public static testMethod void testBatchMerge()

    {

        Test.startTest();

        Opp_BatchProcess b = new Opp_BatchProcess();

        ID myBatchJobID = database.executebatch(b);

        Test.stopTest();

    }

}

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...