0

I've updated my code to do some logic along these lines

  1. if no job has been created, create a job using System.enqueueJob(QC) and store it's job id.
  2. take a bunch of data and store it in a custom object along with the job id.
  3. when the job runs it will query to get the stored data matching the job id.

When I run my test methods however, the job id isn't getting set when I run System.enqueueJob() (I'm checking that using a debug statement right after setting the job Id). When I test manually, the job id is set fine. Is this just something that cannot be done in a test method? Are there other ways to test my code?

Edit: sample code

if(jobId  == null){
           QueueableJob context = new QueueableJob();
           context.isSingletonLogic = true;
           jobId = System.enqueueJob(context);
           System.debug('jobID: ' + jobId);
        }

TestClass

 // Run as User
    System.runAs( user )
    {   
        Trigger_Config__c testCS = TestUtil.getCustomSetting(false,false,false);
        insert testCS;
        Account acc = TestUtil.getAccount();
    }
    System.runAs( userNoPermissions )
    {
        Test.startTest();
        QueueableJob.triggerHelper(actListTest,'Account');
        Test.stopTest();
        List<Custom_Object__c> list = new List<Custom_Object__c>([Select Id from Custom_Object__c]);
        system.assertEquals(0,list.size());
    }
10
  • I just wrote both a queueable and unit test for it, and I am getting a job Id both in the test and in the queueable's context variable. As such, it does appear to be working. Can you share your (not-working) code so we can try to replicate/identify your problem?
    – sfdcfox
    Commented Jun 21, 2021 at 22:07
  • Yeah, I had to modify it to be generic instead of what we have in production since the code is owned by my company but this is the gist of what is giving me problems.
    – J. Larson
    Commented Jun 21, 2021 at 22:20
  • It's not obvious to me how things are even running. Maybe it's an if-else thing in QueueableJob.triggerHelper? Maybe you're calling it wrong somehow? I realize that you can't share your IP (obviously), but we still need more code.
    – sfdcfox
    Commented Jun 21, 2021 at 22:25
  • I know the If statement is running because the debug statement runs, it's just that the id is null when I run it from the test but not null when I do it manually. The if statement is the first thing that runs in the trigger helper method.
    – J. Larson
    Commented Jun 21, 2021 at 22:28
  • All that happens before the testclass code I pasted is a profile gets created, a user gets created, a permission set gets created, and an account gets created. Thank you for helping.
    – J. Larson
    Commented Jun 21, 2021 at 22:34

0

You must log in to answer this question.

Browse other questions tagged .