You need to sign in to do that
Don't have an account?
Please help with Apex Schedule Test Class
Hi Guys and girls,
I have recently written my very first scheduled apex class. This is my first one ever, so far it seems to work. I am trying to write a test class, but I am coming with issues, mainly with code coverage. I cannot write a test capable of passing or if it does pass, it will only test like 24%.
I was hoping to maybe get some tips on:
* My class, any issues that might present themselves
*Perhaps a method of only adding records that have been changed by class to list for update? or does it not matter really?
*Help to write my test class so that it passes atleast 75%
Please any help would be very much appreciated. Thank you in advance.
This is my scheduled apex class:
** * To schedule the Daily reconciliation: * NOTE: It should run at midnight every day on it's own, but if you make * changes and need to requeue run the command below from the developer's console * * scheduledMonthly.scheduleIt(); * NOTE: This class is designed to replace the ServiceSupportReview Time based workflows. */ global class scheduledDaily_ServiceSupportReviews implements Schedulable { public static String CRON_EXP = '0 0 15 * * ? *'; global static String scheduleIt() { scheduledDaily_ServiceSupportReviews sm = new scheduledDaily_ServiceSupportReviews (); return System.schedule('Daily Reconciliation', CRON_EXP, sm); } global void execute(SchedulableContext sc) { //Code goes here List<Service__c> Servs = new List<Service__c>(); Date TodayDate = date.today(); for(Service__c S : [SELECT id, Service_Name__c, End_of_Service_Support__c, X30Days_Review__c, X60Days_Review__c, X40DaysBefore_Review__c, X270Days_Review__c, X180Days_Review__c, X90Days_Review__c, Start_of_Service__c, Account__c FROM Service__c where service_active__c = true]){ If(S.Service_Name__c == 'Intensive Property Coaching'){ //30 Days support Review boolean if ((S.End_of_Service_Support__c).addDays(-335) == date.today() && S.X30Days_Review__c == false) { S.X30Days_Review__c = true; //60 Days support Review boolean } else if ((S.End_of_Service_Support__c).addDays(-305) == date.today() && S.X60Days_Review__c == false) { S.X60Days_Review__c = True; } } If(S.Service_Name__c != 'Essential Property Education Only'){ //90 Days support Review boolean if ((S.End_of_Service_Support__c).addDays(-275) == date.today() && S.X90Days_Review__c == false) { S.X90Days_Review__c = true; //180 Days support Review boolean } else if ((S.End_of_Service_Support__c).addDays(-185) == date.today() && S.X180Days_Review__c == false) { S.X180Days_Review__c = True; //270 Days support Review boolean } else if ((S.End_of_Service_Support__c).addDays(-95) == date.today() && S.X270Days_Review__c == false) { S.X270Days_Review__c = true; //40 Days before end of support } else if ((S.End_of_Service_Support__c).addDays(-40) == date.today() && S.X40DaysBefore_Review__c == false) { S.X40DaysBefore_Review__c = true; } } //Add altered services to a list Servs.add(S); } if (!Servs.isEmpty()) { update Servs; } } }
This is my attempt at a test:
(Currently getting error - System.AsyncException: Based on configured schedule, the given trigger will never fire.
@isTest private class TestSchedulableClass { // CRON expression: midnight on March 15. // Because this is a test, job executes // immediately after Test.stopTest(). public static String CRON_EXP = '0 0 15 15 03 ? 2015'; static testmethod void test() { Account acc = new Account(Name = 'Test Schedule Account'); insert acc; Service__c serTest = new Service__c(Account__c = acc.id); Test.startTest(); // Schedule the test job String jobId = System.schedule('ScheduleApexClassTest', CRON_EXP, new scheduledDaily_ServiceSupportReviews()); // Get the information from the CronTrigger API object CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId]; // Verify the expressions are the same System.assertEquals(CRON_EXP, ct.CronExpression); // Verify the job has not run System.assertEquals(0, ct.TimesTriggered); // Verify the next time the job will run System.assertEquals('2015-03-15 15:00:00', String.valueOf(ct.NextFireTime)); // Verify the scheduled job hasn't run yet. Service__c [] S = [SELECT id, Service_Name__c, End_of_Service_Support__c, X30Days_Review__c, X60Days_Review__c, X40DaysBefore_Review__c, X270Days_Review__c, X180Days_Review__c, X90Days_Review__c, Start_of_Service__c, Account__c FROM Service__c where service_active__c = true]; System.assertEquals(S.size(),0); Test.stopTest(); // Now that the scheduled job has executed after Test.stopTest(), // fetch the new merchandise that got added. // System.assertEquals(ml.size(), 1); } }
Run Your Test Class then GO to Developer Console,In Down panel,there is a tab calles 'Tests', click on your test class and in right side panel you can see 'Overall Code Coverage'. Click on your class name and you will get red lines for uncovered code.
Let us know if it helps.
All Answers
You have defined past date in below line,Please define future date here.
Let us know if it helps you.
Please Mark it as BEST ANSWER so others can get help from it.
Hi Developer.mikie.Apex.Student,
Could you please highlight which all code is not getting passed ?
Hi There, I managed to get 88% code coverage, I had to write the code anew however, the old code would not get above 24%.
Any tips on how I might get the last 12 % or any tips on making the test run smoother would be much appreciated.
Run Your Test Class then GO to Developer Console,In Down panel,there is a tab calles 'Tests', click on your test class and in right side panel you can see 'Overall Code Coverage'. Click on your class name and you will get red lines for uncovered code.
Let us know if it helps.