You need to sign in to do that
Don't have an account?

Test Class Error - Please help
I am testing scheduled jobs in apex class. The test class runs fine in IDE but always errors in the browser/Salesforce UI in the same line. Any help shall be highly appreciated.
static testMethod void testSyncScheduledJobs(){ //initialize logger LoggerSetup loggersetupInstance=new LoggerSetup(); //call the method to schedule the job Test.startTest(); loggersetupInstance.scheduleClearLogger(); Test.stopTest(); //custom setting to store the job ids of the scheduled jobs once scheduled. Set<String> jobInstanceSet=Sched_JobIDs__c.getAll().keySet(); //make sure the job is scheduled System.assertEquals(1,jobInstanceSet.size()); //There can be only a maximum of 25 cron jobs so querying all the records in the cron trigger List<CronTrigger> cronTriggerList=[select Id ,State from CronTrigger LIMIT 100 ]; System.assertEquals(1,cronTriggerList.size()); //after assertion that only one exist, delete the job behind the scenes System.abortjob(cronTriggerList.get(0).id); //now invoke the method to sync the scheduled jobs loggerSetupInstance.syncScheduledJobs(); jobInstanceSet=Sched_JobIDs__c.getAll().keySet(); //make sure the list is empty System.assertEquals(0,jobInstanceSet.size()); }
The error is on the last assert. The expected value is 0 but the actual value is still 1.
Hi,
You can use system.schdule method inside the test method in this way.
Try the below code as reference:
global class SendBuyerAgent2ProfileEmailSchedulable implements Schedulable
{
public SendBuyerAgent2ProfileEmailSchedulable()
{
}
public SendBuyerAgent2ProfileEmailSchedulable(ApexPages.StandardController SC)
{
}
global void execute(SchedulableContext SC)
{
//BUYER AGENT(2) 00eA0000000Z4xp
List<WorkflowEmail__c> WorkflowEmailList = new List<WorkflowEmail__c>();
string HTMLBody = '';
string Subject = '';
for (StaticResource pObjPDF : [SELECT Id,Body, Name,ContentType,Description FROM StaticResource WHERE Name='BuyerAgent2_Email'])
{
Blob StaticResourceBlob = pObjPDF.Body;
HTMLBody = StaticResourceBlob.ToString();
Subject = pObjPDF.Description;
}
for (User pUser : [SELECT Email FROM User WHERE ProfileId='00eA0000000Z4xp' OR ProfileId = '00eA0000000ZXNr'])
{
WorkflowEmail__c NewWE = new WorkflowEmail__c();
//NewWE.Recipient1__c= pUser.Email;
// NewWE.Subject__c = Subject;
//NewWE.Body__c = HTMLBody;
// WorkflowEmailList.add(NewWE);
}
//insert WorkflowEmailList;
}
//Used for Testing Only
global static void SendEmail(string Recipient)
{
for (StaticResource objPDF : [SELECT Id,Body, Name,ContentType,Description FROM StaticResource WHERE Name='BuyerAgent2_Email'])
{
Blob StaticResourceBlob = objPDF.Body;
string HTMLBody = StaticResourceBlob.ToString();
//WorkflowEmail__c NewWE = new WorkflowEmail__c();
//NewWE.Recipient1__c= Recipient;
//NewWE.Subject__c = objPDF.Description;
//NewWE.Body__c = HTMLBody;
//insert NewWE;
}
}
static testmethod void testshow()
{
test.starttest();
SendBuyerAgent2ProfileEmailSchedulable s=new SendBuyerAgent2ProfileEmailSchedulable();
WorkflowEmail__c NewWE=new WorkflowEmail__c();
ApexPages.StandardController sc = new ApexPages.standardController(NewWE);
SendBuyerAgent2ProfileEmailSchedulable obj = new SendBuyerAgent2ProfileEmailSchedulable (sc);
String sch = '0 0 23 * * ?';
system.schedule('Contract Creates', sch, s);
string recipient='abc@test.com';
SendBuyerAgent2ProfileEmailSchedulable.SendEmail(recipient);
test.stopTest();
//s.execute();
}
}
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.