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
Pinky 10Pinky 10 

Hi.. i wrote below test class for Schedulable class and it is giving me error

@isTest
private class TestCheckJitterbitExtract {

    static testMethod void myUnitTest() {
       Test.startTest();

    Batchaccountcountfieldschedule sh1 = new Batchaccountcountfieldschedule();

    String sch = '0 0 2 * * ?';

    system.schedule('Test Territory Check', sch, sh1);

    Test.stopTest();
       
        
    }
}

for 

global class CheckJitterbitExtract implements Schedulable{
    global void execute(SchedulableContext SC) {
        boolean jobRanOK = false;
        DateTime dt = DateTime.now().addHours(-5);
        for(LoginHistory lh : [Select Status, LoginTime, Application From LoginHistory where LoginType = 'Partner Product' and UserId = '00560000001brWwAAI' and LoginTime > :dt]){
            if(lh.Status == 'Success' && lh.Application.contains('Jitterbit')){
                jobRanOK = true;
            }
        }
        if(!jobRanOK)
            throw new JitterbitException('Please check Jitterbit to investigate a possible issue with ToBe extract as there was no connection made to SF today.');
    }
}


 
Best Answer chosen by Pinky 10
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class. In below Test class we already handled the Exception part
@isTest
private class TestCheckJitterbitExtract 
{
    static testMethod void myUnitTest() 
	{
		Test.startTest();
			try
			{
				String CRON_EXP = '0 0 0 3 9 ? 2022';
				String jobId = System.schedule('testBasicScheduledApex',  CRON_EXP,  new CheckJitterbitExtract() );
				CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime  FROM CronTrigger WHERE id = :jobId];
				
				System.assertEquals(TestScheduledApexFromTestMethod.CRON_EXP, ct.CronExpression);			
			}
			Catch(Exception ee)
			{
				Boolean expectedExceptionThrown =  ee.getMessage().contains('Please check Jitterbit to investigate a possible issue with ToBe extract as there was no connection made to SF today')) ? true : false;
				System.AssertEquals(expectedExceptionThrown, true);
			}
		Test.stopTest();
    }
}

If your test class will fail then please post the screen shot of error

All Answers

DeveloperSudDeveloperSud
Hi ,

I think you are creating this test class for schedule class CheckJitterbitExtract but in your test class you have used different name.
Kindly check .I think you need to use  system.schedule('Test Territory Check', sch, new CheckJitterbitExtract () );
Amit Chaudhary 8Amit Chaudhary 8
Please check below post to learn about Scheduler test class
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm

Please try below Scheduler Test class
 
@isTest
private class TestCheckJitterbitExtract 
{
    static testMethod void myUnitTest() 
	{
		Test.startTest();

			String CRON_EXP = '0 0 0 3 9 ? 2022';
			String jobId = System.schedule('testBasicScheduledApex',  CRON_EXP,  new CheckJitterbitExtract() );
	        CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime  FROM CronTrigger WHERE id = :jobId];
			System.assertEquals(TestScheduledApexFromTestMethod.CRON_EXP, ct.CronExpression);			
			
		Test.stopTest();
    }
}

 
Pinky 10Pinky 10
Hi Amit, Thank you for your quick response. The above test is getting failed and throws "Please check Jitterbit to investigate a possible issue with ToBe extract as there was no connection made to SF today." error. I think because i am running it on developer sandbox. Thanks, Priti
Amit Chaudhary 8Amit Chaudhary 8
That is coming because you are throwing exception from your code.
Please try below test class
@isTest
private class TestCheckJitterbitExtract 
{
    static testMethod void myUnitTest() 
	{
		Test.startTest();

			try
			{
				String CRON_EXP = '0 0 0 3 9 ? 2022';
				String jobId = System.schedule('testBasicScheduledApex',  CRON_EXP,  new CheckJitterbitExtract() );
				CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime  FROM CronTrigger WHERE id = :jobId];
				System.assertEquals(TestScheduledApexFromTestMethod.CRON_EXP, ct.CronExpression);			
			}
			Catch(Exception ee)
			{
				
			}
			
		Test.stopTest();
    }
}

Let us know if this will help you
 
Pinky 10Pinky 10
I am getting error on line no. 11 and It's throwing 
JitterbitException: Please check Jitterbit to investigate a possible issue with ToBe extract as there was no connection made to SF today.


 
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class. In below Test class we already handled the Exception part
@isTest
private class TestCheckJitterbitExtract 
{
    static testMethod void myUnitTest() 
	{
		Test.startTest();
			try
			{
				String CRON_EXP = '0 0 0 3 9 ? 2022';
				String jobId = System.schedule('testBasicScheduledApex',  CRON_EXP,  new CheckJitterbitExtract() );
				CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime  FROM CronTrigger WHERE id = :jobId];
				
				System.assertEquals(TestScheduledApexFromTestMethod.CRON_EXP, ct.CronExpression);			
			}
			Catch(Exception ee)
			{
				Boolean expectedExceptionThrown =  ee.getMessage().contains('Please check Jitterbit to investigate a possible issue with ToBe extract as there was no connection made to SF today')) ? true : false;
				System.AssertEquals(expectedExceptionThrown, true);
			}
		Test.stopTest();
    }
}

If your test class will fail then please post the screen shot of error
This was selected as the best answer
Pinky 10Pinky 10
Hi Amit, Here is a screenshot of error. [image: Inline image 1]
Pinky 10Pinky 10
Thank you Amit for you help.