1

I am having issue creating mock for callouts in a queue. I have done following. 

I have a handler (OpportunityTriggerHandler) which creates a job called EnableOnboardingCompleteJob. This job calls a function updateOpportunityStatus in OpportunityWebhooks class. 

public class OpportunityTriggerHandler {
  public static void enableOnboardingCompleted(List opportunity, Map oldOpportunity) {
    for (Opportunity opp : opportunity) {
      // Other codes. 
      EnableOnboardingCompleteJob enableOnboardingJob = new EnableOnboardingCompleteJob(opp);
      System.enqueueJob(enableOnboardingJob);
    }
  }
}

public class EnableOnboardingCompleteJob implements Queueable, Database.AllowsCallouts {
  // other codes
  public void execute(QueueableContext context) {
    OpportunityWebhooks.updateOpportunityStatus(session, opp.Brighte_ID__c); 
  }
}

public class OpportunityWebhooks {
  public static void updateOpportunityStatus(Session s, String id)
  {
    HTTPResponse res = http.send(req); 
  }
}

updateOpportunityStatus makes a callout which has been tested in separate unit test. I have used mock callout for that function and its working perfect. 

I took same approach but when I am testing OpportunityTriggerHandler the mock doesn't work, instead it tries to make real call and fails. 

@isTest
public class OpportunityTriggerHandlerTest {
  @isTest
  public static void testEnableOnboardingCompleted() {

    HttpResponse fakeResponse = new HttpResponse();
    fakeResponse.setStatusCode(204);

    HttpRequest fakeRequest = new HttpRequest();
    fakeRequest.setMethod('POST');
    fakeRequest.setEndpoint('www.example.com/rest/vendor/opportunity/' + opp.Brighte_ID__c);
    test.setMock(HttpCalloutMock.class, new MakeMockHTTPResponse(fakeRequest, fakeResponse));

    Test.startTest();
OpportunityTriggerHandler.enableOnboardingCompleted(newOpportunities, oldOpportunities);
    Test.stopTest();

  }
}

I was wondering if there is any way i can make that call work for queue. Or if there is any way i can just create the job and test that the job is created but not execute it. Or make a fake job create.

​​​​​​​Can you please direct me on right direction please?

Thank you for your help in advance.

  • One related solution i have found is skipping it for testing. salesforce.stackexchange.com/questions/200263/… – Mohammed Fahad Sep 10 at 2:08
  • Can you please include the error message from your test? This ought to work. Skipping the test would be a last resort. Anything which makes your tests less like the real world increases the chance of you missing a real problem – Aidan Sep 10 at 7:13
  • @Aidan, thank you for you response. Sorry for delay in response. First issue that happens is that instead of making fake callout, it tries to make a real call and try to get real response. but in configuration i added dummy data before testing. So the callout fails. When it fails I get a response saying 'You have uncommitted work pending. Please commit or rollback before calling out'. – Mohammed Fahad Sep 17 at 0:51
0

what I would do is the following

@isTest
public class OpportunityTriggerHandlerTest {
    OpportunityTriggerHandler.enableOnboardingCompleted(newOpportunities, oldOpportunities);
    AsyncApexJob[] jobs = [Select Id FROM AsyncApexJob 
                             WHERE ApexClass.Name = `EnableOnboardingCompleteJob`];
    system.assert(!jobs.isEmpty(),'queueable should be created');
}

The above verifies that your triggerhandler schedules a queueable but does not execute it because there is no stopTest(). Thus the callout issues are omitted. You already stated you have a unit test for the queueable's execute() that verifies the callout is done

The only thing my answer does not address is whether you constructed the queueable with the expected object variables. This can be addressed by having the queueable constructor log its inputs to a persistent SObject that you can query later (not a bad practice for async operations in general)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.