-1

This question already has an answer here:

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

}

marked as duplicate by Pranay Jaiswal, Derek F apex Aug 29 at 12:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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. – akarnid Aug 29 at 10:12
0

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 – Pranay Jaiswal Aug 29 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. – Vijay Ganji Aug 29 at 12:16
  • Vijay Thanks for the tip and more info this worked – Gari Vudi Aug 30 at 11:29
  • @GariVudi Glad to help you !! – Vijay Ganji Aug 30 at 11:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.