3

I am implementing Named Credentials to replace how we do callout for a certain endpoint.

One of my current test methods looks something like this:

@isTest
private static void test_method1() {
    MultiStaticResourceCalloutMock multimock = new MultiStaticResourceCalloutMock();
    multimock.setStaticResource('https://api.fakeapi.com/users?login=nomnomnom&api_version=1.1.1.1', 'StaticResourceName');
    multimock.setStaticResource('https://api.fakeapi.com/bakedgoods?api_version=1.1.1.1', 'AnotherStaticResourceName'');
    multimock.setStatusCode(200);
    multimock.setHeader('Content-Type', 'application/json');

    Test.setMock(HttpCalloutMock.class, multimock);
    Test.startTest();

    APIClient client = new APIClient();
    APIClient.APIMethod method1 = client.getSomeInfo('');


    APIClient.APIMethod method2 = client.getSomeOtherInfo('nomnomnom');

    Test.stopTest();

}

Now if I use the old way of populating a URL in my request header like so:

req.setEndpoint('https://api.fakeapi.com/bakedgoods' + bakedgoodId);

Everything works smoothly. As soon as I update the request endpoint in the Client apex class to take a named credential URL like so:

req.setEndpoint('callout:NamedCredentialURL/' + bakedgoodId);

The test class throws a "Static Resource not found: " error.

I have tried many things with fruitless results, I have attempted using the named credential URL in the MultiMock assignment in the test class itself. I have scoured the documentation and nothing has come up to solve this(both from Salesforce and from SE). I would like to avoid using !isRunningTest notation in my client as that seems to be more of duct tape than an actual solution.

I have trimmed the code to have the relevant parts, so something might seem amiss, but I'm open to ideas!

2
  • Seems like this has been an issue for a while.
    – Jayant Das
    Commented Oct 31, 2018 at 19:27
  • 1
    Instead of directly using req.setEndpoint('callout:Named..'), can you try as String endpoint = 'callout:Named..'; req.setEndpoint(endpoint) and see if it works?
    – Jayant Das
    Commented Oct 31, 2018 at 19:39

1 Answer 1

0

If you are specifying a named credential in req.setEndpoint(), then you need to specify exactly the same string when setting up the mocks with MultiStaticResourceCalloutMock.

In other words:

mock.setStaticResource('callout:NamedCredentialName/urlpath?id=11111', 'StaticResourceName');

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .