You need to sign in to do that
Don't have an account?
SFDC16
Test class error for scheduled class
Schedulbale Class:
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();
}
}
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();
}
}
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:
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
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:
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