I have a Schedule job that invokes a class and from there i call the Queueable class like this:
ID jobID = System.enqueueJob(new NSQueuebleJob(acList));
The queueable class is:
public class NSQueuebleJob implements Queueable, Database.AllowsCallouts {
@TestVisible static Boolean makeCallout = true;
private List<Order> orders;
public NSQueuebleJob(List<Order> orders)
{
this.orders = orders;
}
public void execute(QueueableContext qc)
{
if(orders.size() > 0)
{
// do whatever you intended to do
String endpoint = 'url'+orders[0].Pedido__c;
HttpRequest req = new HttpRequest();
req.setEndpoint(endpoint);
req.setMethod('GET');
req.setHeader('Content-Type', 'application/json');
Http http = new Http();
HTTPResponse response = http.send(req);
String jsonText = response.getBody();
Map<String, Object> result = (Map<String, Object>) JSON.deserializeUntyped(jsonText);
String emp = JSON.serialize(result.get('data'));
//Getting data to variables
//update fields
List<Opportunity> listOFOppor = new list<Opportunity>();
for(Opportunity op : [Select fields__c from Opportunity WHERE Id =: orders[0].Oportunidad__c])
{
op.fields__c = orders[0].Pedido__c;
listOFOppor.add(op);
}
update listOFOppor;
if(!Test.isRunningTest()) {
Order order = orders.remove(0);
ID jobID = System.enqueueJob(new NSQueuebleJob(orders));
}
}
The test class is:
@isTest
private class NSQueuebleJobGetSaldoTest {
@isTest static void testCallout() {
StaticTest.setDoNotRunTrigger();
//Do some inserts
Quote testQuoteAfterInsert = [SELECT Empresa__c FROM Quote WHERE Id = :qo.Id];
String norder;
String IdOpp;
List<Opportunity> opList = [SELECT Id FROM Opportunity Where StageName != 'Cerrado Ganado' AND StageName != 'Cancelado'];
List<Order> acList = [SELECT Pedido__c, Oportunidad__c FROM Order WHERE Pedido__c != null AND Oportunidad__c in :opList];
Test.startTest();
Test.setMock(HttpCalloutMock.class, new MockHttpNSQueuebleJobGetSaldoTest());
Test.stopTest();
ID jobID = System.enqueueJob(new NSQueuebleJob(acList));
}
}
And the HttpCalloutMock:
@isTest
global class MockHttpNSQueuebleJob implements HttpCalloutMock {
System.assertEquals('url', req.getEndpoint());
System.assertEquals('GET', req.getMethod());
// Create a fake response
HttpResponse res = new HttpResponse();
res.setHeader('Content-Type', 'application/json');
res.setBody('{"message":null,"data":{"pedido":"001-00108731","error":null,"success":true}');
res.setStatusCode(200);
return res;
}
}
the problem is that im getting this error: You have uncommitted work pending. Please commit or rollback before calling out
And its pointing to this line in the queueable class:
Http http = new Http();
Any suggestions?