How to write test c...
 
Notifications
Clear all

[Solved] How to write test class for schedulable apex with call out in Salesforce

2 Posts
1 Users
0 Likes
719 Views
Posts: 205
 CWL
Admin
Issue starter
(@cwl)
Member
Joined: 11 years ago

Getting the error - Method defined as testMethod do not support web service callouts

Issue Tags
1 Reply
Posts: 205
 CWL
Admin
Issue starter
(@cwl)
Member
Joined: 11 years ago
// Suppose if you have Schedulable apex, which does a callout, with the name SchedulableClass:

global class SchedulableClass implements Schedulable {
    global void execute(SchedulableContext SC) {
        ConnectTo.callRemote();
    }
}

// To schedule it, you run the following code as anonymous apex:

SchedulableClass chpddImp = new SchedulableClass();
String schedule = '0 30 4 * * ?'; // to run every day at 4.30 am
String jobID = system.schedule('Scheduled Job Name', schedule, chpddImp);

// Following will be the test class for it:

@isTest
public class SchedulableClass_Test {
    @isTest static void testSchedulableClass() {
        String schedule = '0 30 4 * * ?';
        Test.setMock(HttpCalloutMock.class, new MockHttpResponseImplementation());
        Test.startTest();
        String jobId = System.schedule('Scheduled Job Name', schedule, new SchedulableClass());
        CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
        Test.stopTest();
        System.assertEquals(schedule, ct.CronExpression);
        System.assertEquals(0, ct.TimesTriggered);
    }
}
Reply
Share: