Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.
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();
}
}
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()).
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...
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
@testSetup
method requires a mock.