+ Start a Discussion

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
        //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
           //There can be only a maximum of 25 cron jobs so querying all the records in the cron trigger
        List<CronTrigger> cronTriggerList=[select Id
                                           from CronTrigger
                                           LIMIT 100
        //after assertion that only one exist, delete the job behind the scenes
        //now invoke the method to sync the scheduled jobs
        //make sure the list is empty

 The error is on the last assert. The expected value is 0 but the actual value is still 1.



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()



    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';







Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.