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
code developercode developer 

Testing Callouts using MultiStaticResourceCalloutMock

Hi,

 

I am using MultiStaticResourceCalloutMock for testing callouts. 

 

I need to use multiple static resource files, so I have zipped the files into single file and uploaded it to static resources.But the setStaticResource() takes only name of staticresource file.

 

The syntax of setStaticResource() is setStaticResource(String endpoint, String resourceName).

 

As mentioned in the syntax we can give only resourceName ,but not path of resource.

 

So,Is there any way to give the path of static resource file to setStaticResource()  or any other alternative ?

 

Any pointers on this will be highly appreciated.

 

 

SForceBeWithYouSForceBeWithYou

As it is currently implemented, the StaticResourceCalloutMock and MultiStaticResourceCalloutMock only take the full String API name of a Static Resource. You will need a separate Static Resource separately named for each mock response.

 

When you are setting the endpoints for your multi mocks, make sure you specify the entire endpoint, including query params:

multimock.setStaticResource('https://api.blah.com/contact?accountid=1234', 'CreateContactSuccess');

 Utilizing previous parts of your responses to create the endpoint strings is about as dynamic as you're going to get with the parameters for the setStaticResource method (unless you want to create static resources with a alphanumeric on the end for a chain of responses).

public static testmethod void testCreateAccountAndContact(){
    String username = 'joedirt';
    String password = 'Passw0rd';
    String acctName = 'Test Account';
    String acctId = '1234';
    String contName = 'Joe Test';
    String contId = '5678';
    
    MultiStaticResourceCalloutMock multimock = new MultiStaticResourceCalloutMock();
    // The query params below dont matter, the response is determined by the static resource
    // However, if the endpoint (including query parameters) doesn't match exactly, 
// it won't map the Static Resource to the endpoint // Below, we populate the endpoint parameters using the variables in this test class
// Static resource was created with username at end multimock.setStaticResource('http://api.blah.com/auth?uname='+username+'&pword='+password, 'BlahApiAuth_'+username); multimock.setStaticResource('http://api.blah.com/account', 'BlahApiCreateAccount'+acctId); multimock.setStaticResource('http://api.blah.com/contact?accountid='+acctId, 'AppNexusCreateContact'+acctId); multimock.setStatusCode(200); multimock.setHeader('Content-Type', 'application/json'); Test.setMock(HttpCalloutMock.class, multimock); Test.startTest(); String authResponse = BlahApi.getAuthResponse(username, password); String authToken = BlahApi.getTokenFromResponse(authResponse); String acctResponse = BlahApi.getCreateAccountResponse(acctName, authToken); ResponseWrapper acctRespWrapper = (ResponseWrapper)JSON.deserialize(ResponseWrapper.class, acctResponse); Integer intAccountId = acctRespWrapper.id; System.assertEquals(String.valueOf(intAccountId), acctId); String contResponse = BlahApi.getCreateContactResponse(contName, intAccountId, authToken); ResponseWrapper contRespWrapper = (ResponseWrapper)JSON.deserialize(ResponseWrapper.class, contResponse); Integer intContactId = contRespWrapper.id; System.assertEquals(String.valueOf(intContactId), contId); }

 

This assumes some sort of ResponseWrapper class that you can deserialize your JSON response with and access response variables.

 

Hope that helps! And...

 

May The SForce Be With You!

youtube.com/MayTheSForceBWithYou

@SForceBeWithYou