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!
req.setEndpoint('callout:Named..')
, can you try asString endpoint = 'callout:Named..'; req.setEndpoint(endpoint)
and see if it works?