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();

    // 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.


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.

global class HttpMockYearFolder implements HttpCalloutMock {

    global HTTPResponse respond(HTTPRequest req) {
        // Create a fake response
        HttpResponse response = new HttpResponse();
        response.setHeader('Content-Type', 'application/json');
        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);


// 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.


  • 1
    It's possible that Box is, in their code, suppressing HTTP requests in test context. Do your tests work without the Mock, or throw an exception due to a callout?
    – David Reed
    Commented Dec 19, 2019 at 14:58
  • Thanks David, tests do not work without the mock and the callout exception is thrown. I did make some progress in finding out that the issue may be to do with the differences in namespaces (Local vs. Box). I was able to get the test to run by also running the "ToolkitTests" test class with my other tests. The "ToolkitTests" test class is part of the Box managed package and seems to include a TestMockCallout method.
    – Daniel
    Commented Jan 6, 2020 at 1:08

2 Answers 2


Ok, I have found the solution. My HttpMock class was in the local namespace but the HttpRequest was made in the Box namespace. Salesforce requires that the mock and request be made in the same namespace.

By changing the following code

// stores the json response from the Box API        
HttpResponse response = toolkit.sendRequest(request);

to this

// If test is running, return standard http object
if(Test.isRunningTest()) {
    Http h = new Http();
    HttpResponse response = h.send(Request);
    return response.getBody();
// If not running a test, use box sendRequest object
else {
    HttpResponse response = toolkit.sendRequest(request);
    return response.getBody();

The httpRequest and httpMock are now called in the name namespace and my tests now work. Hopefully this answer will help anyone else encountering this issue.


Here is a solution that uses a ternary so it does not sacrifice code coverage:

HttpResponse response = Test.isRunningTest() ? new Http().send(request) : toolkit.sendRequest(request);
return response;

You must log in to answer this question.

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