0

I am working on a class that makes a callout to a third party API. The third party API needs to be authenticated with a OpenID Connect Auth token. So, in my class, I am making a callout to the OpenID Connect Auth service to get a bearer token and then I am making a callout to the third party API with the bearer token from OpenID Connect API. The third party API is providing some additional information about the fields on the Account object and I am updating the values on the Account object. There are multiple scenarios in this case.

  1. The values on the Account object is different from what is retrieved from the third party API. In this case, the values on the Account object are updated.
  2. The values on the Account object is same as what is retrieved from the third party API. In this case, the account is not updated.
  3. The third party API returns a blank response. So, the response body contains []. In this case, I am skipping the account.

The apex class is working fine. However, I need some help in building a test class for this. I am sure I need to use MultiStaticResourceCalloutMock stub class to build the test class, but I have some questions around it. My questions are below.

  1. Do I need to write three test methods, one for each scenario?
  2. In each of the test methods, I assume I need to use MultiStaticResourceCalloutMock with two end points, one for OpenID Connect Auth and one for third party API, correct?
  3. I also need three static resources, one for successful response, one for blank response (the static resource file will contain [] only, and one with the response where the values are exactly same as what is on the Account, correct?

Can someone help?

0
0

Do I need to write three test methods, one for each scenario?

Ideally, yes.

In each of the test methods, I assume I need to use MultiStaticResourceCalloutMock with two end points, one for OpenID Connect Auth and one for third party API, correct?

That's one option. However, this class doesn't allow different status codes (e.g. 200 and 401). I prefer my QueueHttpMock design instead, as it allows me to specify the responses without static resources, and a different status code for each callout response. Other designs are also possible.

I also need three static resources, one for successful response, one for blank response (the static resource file will contain [] only, and one with the response where the values are exactly same as what is on the Account, correct?

You have the OAuth response you need too, right? So, that's four in total. You could write your own builder model that's more flexible than MultiStaticResourceCalloutMock.

1
  • Thanks for the response. Yes, I will store the response of OpenID connect auth API into a static resource. – Balaji Pooruli Dec 7 '20 at 4:27
0

To answer each of your questions:

Do I need to write three test methods, one for each scenario?

This is a good idea and you should follow the given - when - then principle of organizing your testmethod. Name the test methods accordingly givenXXXWhenYYYThenZZZ

In each of the test methods, I assume I need to use MultiStaticResourceCalloutMock with two end points, one for OpenID Connect Auth and one for third party API, correct?

YES. You need only code one mock class and pass in to the constructor of the class the scenario you want to mock the response

I also need three static resources, one for successful response, one for blank response (the static resource file will contain [] only, and one with the response where the values are exactly same as what is on the Account, correct?

Not necessarily unless these are really complex responses better managed in a static resource. The mock class can easily return serialized JSON from mock classes, suitably initialized

Example

class MockResponseA {
  String FldA = 'foo';
  String FldB = 'bar';

  String toJson() {return JSON.serialize(this);}
}

and the multiMock class simply generates its response as

theResponse = new MockResponseA().toJson();
3
  • Thanks for your response. I don't get your last statement. Is there an example that I can take a look at? – Balaji Pooruli Dec 7 '20 at 4:27
  • check my revised answer -- I would suggest you accept one of these answers (mine or sfdcfox) and then start developing your multimock class and testmethod; posting a new question if you get stuck – cropredy Dec 7 '20 at 4:34
  • Thanks for your response. The QueueHttpMock was easier and hence I selected that as the best answer. – Balaji Pooruli Dec 7 '20 at 18:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.