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.