Apex Test Coverage with Box api calls

Any tips for testing Box integrations that are used in apex? I’m not sure whether it’s better to

  1. Surround all Box methods/auth/api calls in apex with if(!Test.isRunningTest()) so that those lines of code are skipped?

  2. Create mock HTTP responses to be able to step through the lines of code if a test is running?

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

I can see #2 perhaps being best practice, however we use many different box methods (createFolder(), createFolderForRecordId(), commitChanges(), moveFolder(), getFolderByRecordId(), etc) and this could be very tedious to create custom responses for each one. Thoughts?

Generally speaking for the toolkit it will mock a response if a test is running.

Best practices would be to mock/stub the apex methods Salesforce Developers

If you find that you would rather Mock the HTTP, then I found using static mocks Salesforce Developers easier than handrolling http mocks. You can find JSON examples in the box api docs to save as static files.

1 Like

Thanks David!

I looked at the box-salesforce-sdk source code since it had tests for authentication methods we used. I went off of BoxPlatformApiConnectionTests.testAuthenticate()

Which showed how to set a mock and included this in my box apex methods where auth was used so that if Test.isRunningTest() it would set the mock for the auth the same way. That seemed to help!