1

How to write test class for Schedulable class

Apex class:

global class update_Account implements Schedulable    
{

   public list<Change_Request__c> lstChnReq = new list<Change_Request__c>();
   Map<Id, Account> mapAccount = new Map<Id, Account>();
   public list<Change_Request__c> lstChnReqUpdate = new list<Change_Request__c>();



      global void execute(SchedulableContext sc) 
        {

            Map<String,Schema.RecordTypeInfo> rtMap = Schema.SObjectType.Account.getRecordTypeInfosByName();
            Id rtLeadID =  rtMap.get('Lead').getRecordTypeId();

              lstChnReq = [select Id,Updated__c,Lead_Merchant_Name__c,New_Value__c,Request_Type__c from Change_Request__c where Updated__c= False  and Lead_Merchant_Name__c!= NULL order by CreatedDate asc];
              system.debug('--lstChnReq--'+lstChnReq);    
                  //and Lead_Merchant_Name__r.recordtypeId = :rtLeadID
                  for(Change_Request__c CR:lstChnReq)
                  {

                      Account acc= new Account (id=CR.Lead_Merchant_Name__c); 
                      Change_Request__c chReq = new Change_Request__c(id=CR.Id);
                      if(CR.Request_Type__c=='First Name'){ 
                        acc.FirstName =CR.New_Value__c;
                        chReq.Updated__c= true;

                        system.debug('--CR.Updated__c--'+CR.Updated__c);
                        system.debug('--acc.FirstName--'+acc.FirstName);
                      } 
                      if(CR.Request_Type__c=='Last Name'){
                        acc.LastName =CR.New_Value__c;
                        chReq.Updated__c= true;
                        system.debug('--acc.LastName--'+acc.LastName);
                      }
                      if(CR.Request_Type__c=='Phone'){
                        acc.Phone =CR.New_Value__c;
                        chReq.Updated__c= true;
                        system.debug('--acc.Phone--'+acc.Phone);
                      }

                      if(CR.Request_Type__c=='Alternate Email'){
                        acc.Alternate_Email__c =CR.New_Value__c;
                        chReq.Updated__c= true;
                        system.debug('--acc.Alternate_Email__c--'+acc.Alternate_Email__c); 
                      }
                      /*
                      if(CR.Request_Type__c=='Contact Email'){
                        system.debug('--acc.RecordTypeId--'+acc.RecordTypeId);
                        //if(acc.RecordTypeId == rtLeadID){
                            acc.Subscription_Email__c =CR.New_Value__c; 
                            chReq.Updated__c= true;
                            system.debug('--acc.Subscription_Email__c--'+acc.Subscription_Email__c);
                        //}
                      }
                      */

                      mapAccount.put(acc.id,acc);
                      lstChnReqUpdate.add(chReq);
                  }
                  system.debug('--lstChnReqUpdate--'+lstChnReqUpdate);
                 update mapAccount.values();
                 update lstChnReqUpdate;

          }


    }

Test class:

@isTest 
public class TestUpdate_Account {
    static testmethod void TestUpdate_Account()
    {
        Test.starttest();
        account a = new account();
        a.FirstName='Mark';
        a.lastName='test';
        a.phone='12345678';
        //a.Alternate_Phone__c ='23456789';
        a.Alternate_Email__c ='test@gmail.com';

        insert a;

        Change_Request__c cr = new Change_Request__c();
        cr.Lead_Merchant_Name__c=a.id;
        cr.Request_Type__c='First Name';
        cr.New_Value__c='test';
        cr.Updated__c=true;

        insert cr;
        Test.stoptest();
    }
}

Still my test class passed and 0% coverage how to increase code coverage for scheduler class?

5
  • Is your update_Account class being scheduled through a trigger that you didn't include? If not, you'll need to instantiate the class in your test method, and call system.schedule()
    – Derek F
    Oct 14, 2015 at 18:52
  • I am using cron expression here for schedule class \n update_Account scheUpdateAccount = new update_Account(); String sch ='0 48 * * * ?'; System.schedule('Schedule to update Account Name', sch,scheUpdateAccount);
    – user24524
    Oct 14, 2015 at 18:54
  • Right, but where in your code are those lines? Those 3 lines don't appear in the code that you've provided.
    – Derek F
    Oct 14, 2015 at 18:57
  • Actually I wrote cron expression in execute anonymous in developer console.
    – user24524
    Oct 14, 2015 at 19:00
  • How do you know your test class passed?? You are not doing any assertions and are not testing to see if expected outcomes occurred. All your test did was run without producing an error.
    – Eric
    Oct 14, 2015 at 22:16

2 Answers 2

1

You're not getting coverage because your test isn't scheduling your update_Account class for execution.

Based on the comments in your question, you know how to schedule a class for asynchronous execution.

update_Account scheUpdateAccount = new update_Account(); 
String sch ='0 48 * * * ?'; 
System.schedule('Schedule to update Account Name', sch,scheUpdateAccount);

To get test coverage, these 3 lines of code need to appear in your test method (between Test.startTest() and Test.stopTest()). You could also put those 3 lines of code inside an Insert trigger on Change_Request__c, but you have to be careful when scheduling apex from a trigger.

It also looks like you want to set cr.Updated__c = false in your test setup.

Also, don't forget to put in some assertions into your test. Unit tests aren't very useful without them.

The Apex Scheduler documentation is also a good place to look

1
  • This answer is not complete. All you are doing is executing code. You are not actually testing anything
    – Eric
    Oct 14, 2015 at 22:18
3

Here's how to test a scheduled class

@isTest
    private class testscheduledclass
    {
        public static String CRON_EXP = '0 0 0 15 3 ? 2022';

        static testmethod void test1() 
        {
            Test.startTest();
            String jobId = System.schedule('ScheduleApexClassTest',
                                           CRON_EXP, 
                                           new SCHEDULEDCLASSTOTEST());
            CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime
                              FROM CronTrigger WHERE id = :jobId];
            System.assertEquals(CRON_EXP, 
                                ct.CronExpression);
            System.assertEquals(0, ct.TimesTriggered);
            System.assertEquals('2022-03-15 00:00:00', 
                                String.valueOf(ct.NextFireTime));
            Test.stopTest();
        }
1
  • You should NOT use SEEALLDATA=True except for very specific situation and this is not one of them. I have edited your post to remove that. +1 for the system asserts!
    – Eric
    Oct 14, 2015 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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