Creating a test Class for batch apex

4.1k views Asked by At

I am having truble creating a test class for this particular class. If anyone could provide some code that would implement this I would be very grateful.

Many thanks

Class:

global class TalentIntCustomerBatch implements Database.Batchable<sObject>, Database.AllowsCallouts{
    global final String query;

    global TalentIntCustomerBatch(String q){
        query=q;
    }

    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<sObject> scope){     
        for(sObject s : scope){
            Contact c = (Contact)s;
            TalentIntegrationUtils.updateCustomer(c.Id, c.LastName);
        }
    }

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

There are 1 answers

0
Kyle Olson On

You will need to populate the data in test to create the contacts and any other objects your TalentIntegrationUtils class needs, but the following code should work to test it:

string query = 'Select Id, LastName From Contact';
TalentIntCustomerBatch ticb = new TalentIntCustomerBatch(query);
Database.executeBatch(ticb);

From the name of your class, you may be making call outs to external systems during the test. If this is the case you will either need to add an "if (Test.isRunningTest() == false)" block around all of your call outs or implement a mock response:

Testing Web Service Callouts

Testing HTTP Callouts by Implementing the HttpCalloutMock Interface