function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Terri HenryTerri Henry 

Test Class for BoxToolkit Callout

Hi Everyone,

I've got a class that uses Box's Toolkit to make a callout to change a folder's name - it's invocable and is triggered on Salesforce record name change (and works!)

Coming into difficultly running the test class as I'm getting the error message of "System.TypeException: Supplied type is not an interface" when running the test class with the Stack Trace of "(System Code)
Class.BoxFolderRenamerTest.testRenameBoxFolder: line 34, column 1"

Any ideas how to change the test class to provide coverage?
 
public class BoxFolderRenamer {
    
    @InvocableMethod(label='Rename Box Folder' description='Renames a Box folder')
    public static void renameBoxFolder(List<BoxFolderRenameRequest> requests) {
        
        // Instantiate the Toolkit object
        box.Toolkit boxToolkit = new box.Toolkit();
        
        for (BoxFolderRenameRequest request : requests) {
            // Prepare the API endpoint URL for folder renaming
            String endpointUrl = 'https://api.box.com/2.0/folders/' + request.folderId;
            
            // Create a Map for the new folder name
            Map<String, Object> folderUpdate = new Map<String, Object>();
            folderUpdate.put('name', request.newName);
            
           // Create a new HttpRequest object and set appropriate values
            HttpRequest httpRequest = new HttpRequest();
            httpRequest.setHeader('Content-Type', 'application/json');
            httpRequest.setEndpoint(endpointUrl);
            httpRequest.setMethod('PUT');
            httpRequest.setBody(JSON.serialize(folderUpdate));
            
            //HttpResponse httpResponse = new Http().send(httpRequest);
            // Send the HttpRequest through the generic Toolkit method, which will handle the authentication details
            HttpResponse httpResponse = boxToolkit.sendRequest(httpRequest);
            
            // Handle the response as needed
            if (httpResponse.getStatusCode() != 200) {
                System.debug('Response Code: ' + httpResponse.getStatusCode() + '. Response Status: ' + httpResponse.getStatus());
                system.debug('most recent error: ' + boxToolkit.mostRecentError);
            }
        }
        
        // ALWAYS call this method when finished. Since salesforce doesn't allow http callouts after dml operations, we need to commit the pending database inserts/updates or we will lose the associations created
        boxToolkit.commitChanges();
    }
    
    // Wrapper class for the Box folder rename request - variables to be passed from Flow
    public class BoxFolderRenameRequest {
        @InvocableVariable(label='Folder ID' required=true)
        public String folderId;
        
        @InvocableVariable(label='New Folder Name' required=true)
        public String newName;
    }
}

Test Class
@isTest
public class BoxFolderRenamerTest {
    // Create a mock HTTP callout response
    public class MockHttpResponse implements HttpCalloutMock {
        public HTTPResponse respond(HTTPRequest request) {
            HttpResponse httpResponse = new HttpResponse();
            httpResponse.setHeader('Content-Type', 'application/json');
            httpResponse.setBody('{"status": "success"}');
            httpResponse.setStatusCode(200);
            return httpResponse;
        }
    }
    @isTest
    public static void testRenameBoxFolder() {
        // Create test data
        List<BoxFolderRenamer.BoxFolderRenameRequest> testRequests = new List<BoxFolderRenamer.BoxFolderRenameRequest>();
        // Add test request(s)
        BoxFolderRenamer.BoxFolderRenameRequest request1 = new BoxFolderRenamer.BoxFolderRenameRequest();
        request1.folderId = 'folderId';
        request1.newName = 'New Folder Name';
        testRequests.add(request1);
        // Set the mock HTTP callout response
        Test.setMock(HttpCalloutMock.class, new MockHttpResponse()); 
        // Invoke the renameBoxFolder method
        Test.startTest();  
        BoxFolderRenamer.renameBoxFolder(testRequests); 
        Test.stopTest();
    }
}


 
SubratSubrat (Salesforce Developers) 
Hello Terri ,

The error "System.TypeException: Supplied type is not an interface" occurs because you are trying to set the mock HTTP callout response using the wrong class in your test class.

Reference -> https://developer.salesforce.com/forums/?id=9060G000000I4M0QAK

In your test class, you are trying to set the mock HTTP callout response using HttpCalloutMock.class, which is incorrect. Instead, you should set it using the MockHttpResponse class that you have defined within the test class.

Here's the corrected test class:
@isTest
public class BoxFolderRenamerTest {
    // Create a mock HTTP callout response
    public class MockHttpResponse implements HttpCalloutMock {
        public HTTPResponse respond(HTTPRequest request) {
            HttpResponse httpResponse = new HttpResponse();
            httpResponse.setHeader('Content-Type', 'application/json');
            httpResponse.setBody('{"status": "success"}');
            httpResponse.setStatusCode(200);
            return httpResponse;
        }
    }
    
    @isTest
    public static void testRenameBoxFolder() {
        // Create test data
        List<BoxFolderRenamer.BoxFolderRenameRequest> testRequests = new List<BoxFolderRenamer.BoxFolderRenameRequest>();
        
        // Add test request(s)
        BoxFolderRenamer.BoxFolderRenameRequest request1 = new BoxFolderRenamer.BoxFolderRenameRequest();
        request1.folderId = 'folderId';
        request1.newName = 'New Folder Name';
        testRequests.add(request1);
        
        // Set the mock HTTP callout response
        Test.setMock(HttpCalloutMock.class, new MockHttpResponse()); 
        
        // Invoke the renameBoxFolder method
        Test.startTest();  
        BoxFolderRenamer.renameBoxFolder(testRequests); 
        Test.stopTest();
        
        // Perform assertions or additional testing if required
    }
}

If this helps , please mark this as Best Answer.
Thank you.
Terri HenryTerri Henry
Hi Subrat - thanks for your response, but the corrected test class you've provided is the same as mine - I'm already using:
Test.setMock(HttpCalloutMock.class, new MockHttpResponse());