0

I've written this method to create my custom Opportunity folder in box. It is working and doing what I expect it to do. So now I am writing the test method but do not know how to deal with the methods that come from the box toolkit for developers.

My method:

public String createOpportunityFolder(){

        // get record details where button was clicked - which Opportunity and which account does it belong to
        Opportunity opp = (Opportunity) standardController.getRecord();
        Id accountId = opp.AccountId;
        List<Account> accountquery = [SELECT Id, Name, BoxFolderIdOpportunity__c FROM Account WHERE Id= :accountId];

        String accountFolderId;

        // instantiate new box toolkit object
        box.Toolkit boxToolkit = new box.Toolkit();

        // check that only one account is returned
        if (accountquery.size()==1){
            Account account = accountquery[0];

            // get folder Id of account to indicate parent folder or create a folder if none exists
            accountFolderId = boxToolkit.createFolderForRecordId(accountId, account.Name, false);
            System.debug('Box Folder Id of Account: '+accountFolderId);

            // check for errors with account box folder
            if (accountFolderId == null){
                System.debug('most recent error: '+ boxToolkit.mostRecentError);
            }
            else{
                // DO MORE
            }
     }

My test method:

@isTest static void testCreateOpportunityFolder(){
        // test data setup
        // create an account with an opportunity and try to create a opportunity folder for that account (in box; but callouts are not done in tests)
        Account acct = new Account(Name='Test Account');
        insert acct;
        Opportunity opp = new Opportunity(Name=acct.Name + ' Opportunity',
                                          StageName='Qualify',
                                          CloseDate=System.today().addMonths(1),
                                          AccountId=acct.Id);
        insert opp;


        // CALL CONSTRUCTOR INSIDE THE TEST RUN?
        //Test.setCurrentPageReference(new PageReference('Page.BoxFoldersController'));
        //ApexPages.currentPage().getParameters().put('lc',opp.id);
        ApexPages.StandardController stdController = new ApexPages.StandardController(opp);        
        BoxFoldersController boxController = new BoxFoldersController(stdController);

        // perform test
        Test.startTest();
        // call method to test
        String folderResult = boxController.createOpportunityFolder();
        Test.stopTest();
}

The problem is in the line with accountFolderId = boxToolkit.createFolderForRecordId(...); which is always returning null and sending this error message:

|DEBUG|most recent error: Unable to get the Salesforce root folder.  Has the root folder been set in Box Settings?

Can someone help or point me to a direction that can explain why while running the code the root folder can be found and while running the test it cannot be found?

EDIT

Alright, data is not available in test runs. So added those lines before testing my method

box__Folder_Details__c salesforceRF = new box__Folder_Details__c(Name='Salesforce', box__FolderId__c='9999999999');
        insert salesforceRF;

The new error message is the following

 Most recent error: Unable to create folder for Salesforce record because the root folder was not found/created.

So is my test really trying to create folders??

  • Can you share the managed package with us on the AppExchange? It's entirely possible some of the package code can't run successfully in a unit test. IMO that's a flaw of the package if that's the case, but you can get around it with Test.isRunningTest() or by using dependency injection. It's possible there is a mock version of the Toolkit or a mock method in the Toolkit to use. – nbrown May 28 at 19:33
  • it is Box for Salesforce and I am using the dev toolkit that is already installed (appexchange.salesforce.com/…) – steph May 30 at 19:53
  • Unfortunately I'm not seeing anything about testing in the developer documentation with the source code. You might get a quicker response at the Box developer forum. I am also not seeing the error message you received in the code base or on the help documents at Box - are you sure the error message is from the SDK and not custom code your project has implemented? – nbrown Jun 1 at 13:12
  • I'm not using the SDK but the box developer toolkit. It is already available in Apex with no need to install anything. The debug is printing the "most recent error" from box toolkit so I am not sure what you mean by code my project has implemented? The reference I am using is here developer.box.com/guides/tooling/salesforce-toolkit/methods – steph Jun 2 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.