1

I have a Schedulable class which is scheduled to execute every night at 12:00 AM.

Here is my class:

public class appdevDailySnapShot implements Schedulable{
    public List<App_Dev__c> ac {set;get;}    
    public void execute(SchedulableContext con){
        List<App_Dev_Log__c> lstADL = new List<App_Dev_Log__c>();
        try
        {               
           ac=[select Name,Assigned_To__c,Category__c,Completed_date__c,Effort_Hrs__c,Environment__c,Expected_QA_Date__c,In_Progress_Date__c,Old_Create_Date__c,Old_Request__c,On_Hold_date__c,Priority__c,Production_Release_Date__c,Project__c,Ready_for_Production_Release__c,Released_in_QA_Date__c,Released_in_UAT_date__c,Requested_QA_Release_Date__c,Status__c,SVN_update__c,Type__c,Unit_testing_DEV__c,Verified_in_Production__c,Verified_in_QA_Date__c,Verified_in_UAT_date__c from App_Dev__c];            
            for(App_Dev__c adc : ac)
            {
                App_Dev_Log__c AL = new App_Dev_Log__c();
                AL.AD_Request_No__c = adc.Name;
                AL.Assigned_To__c = adc.Assigned_To__c;
                AL.Category__c = adc.Category__c;
                AL.Completed_date__c = adc.Completed_date__c;
                AL.Effort_Hrs__c = adc.Effort_Hrs__c;
                AL.Environment__c = adc.Environment__c;
                AL.Expected_QA_Date__c = adc.Expected_QA_Date__c;
                AL.In_Progress_Date__c = adc.In_Progress_Date__c;
                AL.Old_Create_Date__c = adc.Old_Create_Date__c;
                AL.Old_Request__c = adc.Old_Request__c;
                AL.On_Hold_date__c = adc.On_Hold_date__c;
                AL.Priority__c = adc.Priority__c;
                AL.Production_Release_Date__c = adc.Production_Release_Date__c;
                AL.Project__c = adc.Project__c;
                AL.Ready_for_Production_Release__c = adc.Ready_for_Production_Release__c;
                AL.Released_in_QA_Date__c = adc.Released_in_QA_Date__c;
                AL.Released_in_UAT_date__c = adc.Released_in_UAT_date__c;
                AL.Requested_QA_Release_Date__c = adc.Requested_QA_Release_Date__c;
                AL.Status__c = adc.Status__c;
                AL.SVN_update__c = adc.SVN_update__c;
                AL.Type__c = adc.Type__c;
                AL.Unit_testing_DEV__c = adc.Unit_testing_DEV__c;
                AL.Verified_in_Production__c = adc.Verified_in_Production__c;
                AL.Verified_in_QA_Date__c = adc.Verified_in_QA_Date__c;
                AL.Verified_in_UAT_date__c = adc.Verified_in_UAT_date__c;
                AL.Snapshot_Date__c = date.today();
                AL.Snapshot_Time__c = datetime.now();
                lstADL.add(AL);
            }        
            insert(lstADL);
        }
        catch(Exception pEx){
            System.debug('**** Exception Inserting in APP_DEV_Log ****');
            System.debug(pEx.getMessage());
        }
        finally{
            lstADL.clear();
            lstADL = null;
        }
    }   
}

Here is my test class:

@istest
public class appdevDailySnapShot_Test {
    static testMethod void testSample1(){
        Test.startTest();
        Datetime dt2 = Datetime.now().addMinutes(1);
        String CRON_EXP2 = '0 '+ dt2.minute() + ' * ' + dt2.day() + ' ' + dt2.month() + ' ? ' + dt2.year();
        Id jobId = System.schedule('AppDev Daily Snapshot - Test1', CRON_EXP2, new appdevDailySnapShot());
        Test.stopTest();
        System.assert(jobId != null);
    }
    static testMethod void testSample2() {
        Test.startTest();
        Datetime dt = Datetime.now().addMinutes(1);
        appdevDailySnapShot aObj = new appdevDailySnapShot();
        String CRON_EXP = '0 '+ dt.minute() + ' * ' + dt.day() + ' ' + dt.month() + ' ? ' + dt.year();
        String jobId = System.schedule('AppDev Daily Snapshot - Test2', CRON_EXP, aObj);   
        Test.stopTest();
    }
} 

My code coverage is 22%.

1

I don't totally trust the scheduling code you have here and would recommend moving execute code into a private method that is marked with @testVisible attribute.

public class appdevDailySnapShot implements Schedulable {
    public List<App_Dev__c> ac { set; get; }

    // implement the Schedulable interface
    public void execute(SchedulableContext con) {
        execute();
    }

    // this method is testable
    @TestVisible
    private void execute() {
        List<App_Dev_Log__c> lstADL = new List<App_Dev_Log__c>();
        try
        {
            ac = [select Name,Assigned_To__c,Category__c,Completed_date__c,Effort_Hrs__c,Environment__c,Expected_QA_Date__c,In_Progress_Date__c,Old_Create_Date__c,Old_Request__c,On_Hold_date__c,Priority__c,Production_Release_Date__c,Project__c,Ready_for_Production_Release__c,Released_in_QA_Date__c,Released_in_UAT_date__c,Requested_QA_Release_Date__c,Status__c,SVN_update__c,Type__c,Unit_testing_DEV__c,Verified_in_Production__c,Verified_in_QA_Date__c,Verified_in_UAT_date__c from App_Dev__c];          
            for(App_Dev__c adc : ac)
            {
                App_Dev_Log__c AL = new App_Dev_Log__c();
                AL.AD_Request_No__c = adc.Name;
                AL.Assigned_To__c = adc.Assigned_To__c;
                AL.Category__c = adc.Category__c;
                AL.Completed_date__c = adc.Completed_date__c;
                AL.Effort_Hrs__c = adc.Effort_Hrs__c;
                AL.Environment__c = adc.Environment__c;
                AL.Expected_QA_Date__c = adc.Expected_QA_Date__c;
                AL.In_Progress_Date__c = adc.In_Progress_Date__c;
                AL.Old_Create_Date__c = adc.Old_Create_Date__c;
                AL.Old_Request__c = adc.Old_Request__c;
                AL.On_Hold_date__c = adc.On_Hold_date__c;
                AL.Priority__c = adc.Priority__c;
                AL.Production_Release_Date__c = adc.Production_Release_Date__c;
                AL.Project__c = adc.Project__c;
                AL.Ready_for_Production_Release__c = adc.Ready_for_Production_Release__c;
                AL.Released_in_QA_Date__c = adc.Released_in_QA_Date__c;
                AL.Released_in_UAT_date__c = adc.Released_in_UAT_date__c;
                AL.Requested_QA_Release_Date__c = adc.Requested_QA_Release_Date__c;
                AL.Status__c = adc.Status__c;
                AL.SVN_update__c = adc.SVN_update__c;
                AL.Type__c = adc.Type__c;
                AL.Unit_testing_DEV__c = adc.Unit_testing_DEV__c;
                AL.Verified_in_Production__c = adc.Verified_in_Production__c;
                AL.Verified_in_QA_Date__c = adc.Verified_in_QA_Date__c;
                AL.Verified_in_UAT_date__c = adc.Verified_in_UAT_date__c;
                AL.Snapshot_Date__c = date.today();
                AL.Snapshot_Time__c = datetime.now();
                lstADL.add(AL);
            }       
            insert(lstADL);
        }
        catch(Exception pEx) {
            System.debug('**** Exception Inserting in APP_DEV_Log ****');
            System.debug(pEx.getMessage());
        }
        finally {
            lstADL.clear();
            lstADL = null;
        }
    }
}

Also, you have to insert a App_Dev__c record. Test code does not see actual data in the system, so you have to add a record in order for query to return some results.

@istest
public class appdevDailySnapShot_Test {

    static addTestData() {
        App_Dev__c ad = new App_Dev__c();
        ad.Name = 'app dev';
        // maybe set other properties?
        insert ad;
    }

    static testMethod void testCanBeScheduled() {
        addTestData(); // insert test data before running cron job
        Test.startTest();
        Datetime dt2 = Datetime.now().addMinutes(1);
        String CRON_EXP2 = '0 '+ dt2.minute() + ' * ' + dt2.day() + ' ' + dt2.month() + ' ? ' + dt2.year();
        Id jobId = System.schedule('AppDev Daily Snapshot - Test1', CRON_EXP2, new appdevDailySnapShot());
        Test.stopTest();
        System.assertNotEquals(null, jobId);
    }

    static testMethod void testCheckResults() {
        addTestData(); // insert test data before running cron job
        Test.startTest();
        appdevDailySnapShot aObj = new appdevDailySnapShot();
        aObj.execute();
        Test.stopTest();

        // query App_Dev_Log__c for expected results
        List<App_Dev_Log__c> lstADL = [select AD_Request_No__c from App_Dev_Log__c];
        System.assertEquals(1, aObj.ac.size());
        System.assertEquals(1, lstADL.size());
        System.assertEquals(aObj.ac[0].Name, lstADL[0].AD_Request_No__c);
        // check additional properties here (must add to above query)
    }
} 
  • No assertions in your second method. Also note that there is an assertNotEquals(expected, actual) method, which is the preferred approach over assert(expected != actual). – Adrian Larson May 2 '17 at 17:40
  • Good suggestions :) I've updated my code here. – dana May 2 '17 at 17:53
  • Thanks guys .. It worked . Currently code coverage of the Apex class increased to 97% . Could you guys help me to reach 100 % . Also I have an other issue , Navigating to Apex Classes it says [ overall code coverage is currently 21%. To deploy code to production , you must need at least 75% ] . Could you please guide to resolve this as well – Ermin Tsikkos May 2 '17 at 19:14

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