global class converterSchedule implements Schedulable {
global void execute(SchedulableContext ctx) {
List<Exchange_rate__c> exchange = [SELECT Date__c, GBP__c, USD__c, EUR__c, CAD__c
FROM Exchange_rate__c];
converterWidgetController.requestAndResponseCurrency();
}
}
-
You need to read up on cron expressions for the test. you can start off by looking at the sample test class in this post salesforce.stackexchange.com/questions/102074/… . When I had to do this is just based mine off this setup. It's fairly easy to understand. Otherwise read up on the4 Schedulable interface in Salesforce support documents.– akarnidCommented Aug 29, 2019 at 10:12
1 Answer
You can go through Asynchronous Apex trailhead module to learn how to write test classes for Scheduled classes.
For writing a test class for scheduled class, you need to use startTest
and stopTest
around the System.schedule
method, to ensure processing finishes before continuing your test.
For your case, your test class should be something like below
@isTest
private class converterScheduleTest {
static testmethod void schedulerTest() {
String CRON_EXP = '0 0 0 15 3 ? *';
List<Exchange_rate__c> exchangeRates = new List<Exchange_rate__c>();
// Create your test data
Exchange_rate__c ex1 = new Exchange_rate__c();
ex1.name= 'test';
exchangeRates.add(ex1);
Exchange_rate__c ex2 = new Exchange_rate__c();
ex2.name= 'test';
exchangeRates.add(ex2);
insert exchangeRates;
Test.startTest();
String jobId = System.schedule('ScheduleJobName', CRON_EXP, new converterSchedule());
CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
System.assertEquals(CRON_EXP, ct.CronExpression);
System.assertEquals(0, ct.TimesTriggered);
Test.stopTest();
// Add assert here to validate result
}
}
You will have to set the values of all other fields of Exchange_Rate__c
object i.e. Date__c, GBP__c, USD__c, EUR__c, CAD__c
if they are referred in your code.
-
1
give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime
Commented Aug 29, 2019 at 12:01 -
@PranayJaiswal well I do agree with you, but in my answer, I don't think it is the final solution for his problem (he still has to test/cover his functionality), it is just a heads up for the OP to know how to write a test class for scheduled class which is what is already present in the trailhead module mentioned in the answer. Commented Aug 29, 2019 at 12:16
-
-