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
SFDC16SFDC16 

Test class error for scheduled class

Schedulbale Class:

User-added image

global class DeletAccount implements Schedulable
{
     
     global void execute(SchedulableContext ctx)
     {
         list<Account> acc=new list<Account>();
         for(Account acc1:[Select id,name from account where name like '%Demo%'])        
             acc.add(acc1);--------------->Error Line
             
   
         delete acc;      
     }
       
}

Test Class:

@isTest
public class Testclass {
 public static testmethod void test1()
 {
     Test.startTest();
           list<account> acc=new list<Account>(); 
        Account a1 = new Account();
          a1.name='Dummy account';
         acc.add(a1);    
         insert acc; 
        String CRON_EXP = '0 0 0 3 9 ? 2022';
        String jobId = System.schedule('test111', CRON_EXP, new DeletAccount());
         CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
          System.debug('ct'+ct);
        System.assertEquals(0, ct.TimesTriggered);
        System.assertEquals('2022-09-03 00:00:00', String.valueOf(ct.NextFireTime));
     Test.stopTest();  
 }
                
}
 
Best Answer chosen by SFDC16
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

In business class, you are fetching account where name consists of Demo. So, in a test class, you need to insert account name which has 'demo' in it.

Try below code:
@isTest
public class Testclass {
 public static testmethod void test1()
 {
     Test.startTest();
           list<account> acc=new list<Account>(); 
        Account a1 = new Account();
          a1.name='Dummy Demo account';
         acc.add(a1);    
         insert acc; 
        String CRON_EXP = '0 0 0 3 9 ? 2022';
        String jobId = System.schedule('test111', CRON_EXP, new DeletAccount());
         CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
          System.debug('ct'+ct);
        System.assertEquals(0, ct.TimesTriggered);
        System.assertEquals('2022-09-03 00:00:00', String.valueOf(ct.NextFireTime));
     Test.stopTest();  
 }
                
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

In business class, you are fetching account where name consists of Demo. So, in a test class, you need to insert account name which has 'demo' in it.

Try below code:
@isTest
public class Testclass {
 public static testmethod void test1()
 {
     Test.startTest();
           list<account> acc=new list<Account>(); 
        Account a1 = new Account();
          a1.name='Dummy Demo account';
         acc.add(a1);    
         insert acc; 
        String CRON_EXP = '0 0 0 3 9 ? 2022';
        String jobId = System.schedule('test111', CRON_EXP, new DeletAccount());
         CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
          System.debug('ct'+ct);
        System.assertEquals(0, ct.TimesTriggered);
        System.assertEquals('2022-09-03 00:00:00', String.valueOf(ct.NextFireTime));
     Test.stopTest();  
 }
                
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
SFDC16SFDC16
thanks, khan its working.