function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nishit MistryyyNishit Mistryyy 

Write a test class scheulde

I have one class that is defined, I want to write a test class for it with an EXPLANATION!!
The code for the apex class is :

global class ScheduleToUpdateMilestoneTargetdate implements schedulable{
    global void execute(Schedulablecontext sc)
    {
        BatchClassToUpdateCaseObject cases=new BatchClassToUpdateCaseObject();
        if(!Test.isRunningTest()){
            Database.executeBatch(cases,200);
        }
    }  
}

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Nitish,

Your class only gets 75% coverage because the below code cannot be covered.
Because this piece of code will not run in test class hence we loose the covearage for this
if(!Test.isRunningTest()){
            Database.executeBatch(cases,200);
        }

We are just initilizing thr schedular class and setting to schedule this at particualr time
 
@isTest
public class TestAccount {
static testMethod void myUnitTest()  {
        
        Test.StartTest();
ScheduleToUpdateMilestoneTargetdate sh1 = new ScheduleToUpdateMilestoneTargetdate();

String sch = '0 0 23 * * ?';
    system.schedule('Test Territory Check', sch, sh1);
    Test.stopTest(); 
           

    }
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,