1

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?

0

I'd suggest breaking each functional part down and testing them in isolation rather than trying to do a complete integration test in one go.

E.g. Test the Queueable class in isolation from the Scheduled job that calls it. You could even test the code that makes the callout in isolation from the Queueable class.

See also: Testing scheduled apex containing @future method?

0

Ok finally i fix the error. The solution:

add this to the test

@isTest(SeeAllData=true)
  • Using seeAllData=true is strongly discouraged because it makes your tests more fragile and breaks test data isolation (your test ends up having a dependency on the data inside the org it's running in). A better approach is to create all of the data you need for a unit test inside of the test class itself. This not only prevents you from potentially altering real data, but also allows your tests to run in a different org (like, say, a sandbox or scratch org that you just provisioned). – Derek F Sep 20 at 18:37

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.