-3
public class QbPaymentConnect {
    
    @future(callout=true)
    public static void QbCustomers(string recordId){
        opportunity op = [select name from opportunity where id=:recordId];
        system.Debug(op);
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setMethod('POST');  
        request.setEndpoint('callout:qbOnline/v3/company/4620816365248252120/customer?minorversion=40');
        request.setHeader('Content-Type','application/json');  
        request.setHeader('Accept', 'application/json');
        string jsonString='{"DisplayName" :"'+op.name +'"}';
        request.setBody(jsonString);
        HTTPResponse res = http.send(request);
        system.debug('res:'+res.getBody());
        //system.debug('Status Code: '+res.getStatusCode());
        QuickBookWrapperClass cus = new QuickBookWrapperClass();
        cus = (QuickBookWrapperClass)JSON.deserialize(res.getBody(), QuickBookWrapperClass.class);
        String custId = cus.Customer.Id;
        op.customerId__c=custId;
        update op;
        system.debug('customer Id: '+custId);
        
    }
}
global class mockCalloutQB implements HttpCalloutMock{
    global HttpResponse respond(HttpRequest req){
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type','application/json');
        res.setBody('{"Customer": {"Id": "67"}}');
        res.setStatusCode(200);
        return res;
    }
}
isTest
public class testQB {
    @testSetup static void setup() {
        Opportunity op =   new Opportunity(Name = 'TestOpp',closeDate = System.today(),stageName='Prospecting');        
        insert op;
    }
    
    @isTest 
    private static void QbCustomerTest(){
        Test.startTest();
        opportunity op = [select Name,id from opportunity where name like 'TestOpp'];
        string recordId = op.Id;
        Test.setMock(HttpCalloutMock.class, new mockCalloutDrive());
        
        QbPaymentConnect.QbCustomers(recordId);
        Test.stopTest();

        
    }
}
1

1 Answer 1

1

You need to set a mock. See Testing HTTP Callouts:

By default, test methods don’t support HTTP callouts, so tests that perform callouts fail. Enable HTTP callout testing by instructing Apex to generate mock responses in tests, using Test.setMock.

See also Testing HTTP Callouts by Implementing the HttpCalloutMock Interface. Like much Salesforce documentation, it uses global where it is not necessary, and you should instead use public. But all you really need to know is that you must implement the HttpCalloutMock interface. Once you've set up such a class, you pass an instance to Test.setMock(HttpCalloutMock.class, new MyMock()).

2
  • its already there in the code by the name mockCalloutDrive. Oct 28, 2022 at 17:03
  • Is the code in your question your actual code or paraphrased? Seems like you must be causing a callout before your mock is set. You could try moving it to the very top of your test method to verify...
    – Adrian Larson
    Oct 28, 2022 at 18:29

Not the answer you're looking for? Browse other questions tagged .