1

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

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm

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

  • 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 Dec 19 '19 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 Jan 6 at 1:08
1

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.

| improve this answer | |

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.