Wanted to see if anyone has encountered/solved this issue.
We have the Box.com app installed in Salesforce (Lightning, Enterprise edition) and I’ve written a function that makes a call to the Box.com API to get a list of items in an account’s folder when an opportunity is created. A copy of the code that makes the callout to the Box.com API is shown below.
// returns JSON list of folder items
public static string getBoxFolderItems(string folderId) {
// Instantiate the Toolkit object
box.Toolkit toolkit = new box.Toolkit();
// Sets endpoint to Box API call using the account's folder id
string endpoint = 'https://api.box.com/2.0/folders/' + folderId + '/items';
// Creates httprequest
HttpRequest request = new HttpRequest();
request.setMethod('GET');
request.setEndpoint(endpoint);
// stores the json response from the Box API
HttpResponse response = toolkit.sendRequest(request);
// returns
return response.getBody();
}
This method uses the sendRequest generic method which is included in the Box Salesforce Developer Toolkit. Link to the documentation below.
https://developer.box.com/en/guides/tooling/salesforce-toolkit/methods/#sendrequest
This sendRequest method handles the authentication with the Box.com API. This all works without issue. The issue I have encountered is creating a successful test class. As HttpRequests do not work in a test class, I created a httpMock class based on the documentation (link below).
I have also included a copy of my HttpMock class below.
@isTest
global class HttpMockYearFolder implements HttpCalloutMock {
global HTTPResponse respond(HTTPRequest req) {
// Create a fake response
HttpResponse response = new HttpResponse();
response.setHeader('Content-Type', 'application/json');
response.setBody('{"total_count":1,"entries":[{"type":"folder","id":"92176615703","sequence_id":"0","etag":"0","name":"2019-2020"}],"offset":0,"limit":100,"order":[{"by":"type","direction":"ASC"},{"by":"name","direction":"ASC"}]}');
response.setStatusCode(200);
return response;
}
}
The problem I have identified is that by using the toolkit.sendRequest(request) method it will not cause the httpMock class to be called.
If I change
// stores the json response from the Box API
HttpResponse response = toolkit.sendRequest(request);
To
// stores the json response from the Box API
Http h = new Http();
HttpResponse response = h.send(Request);
The HttpMock will successfully work and the test will succeed.
My question is has anyone created a test class which can handle the sendRequest method and is the HttpMock class the correct approach to this? I have spent about a week looking into this as I wanted to make sure I sufficiently researched the problem but I have not found anything that could help in the Box/Salesforce documentation, posts on the Salesforce developer forums and Salesforce Stack Exchange and the Box/Salesforce GitHub account. I also submitted a ticket to Box.com’s support team and spoke to a contact at Box but wasn’t about to obtain a solution.
Cheers