'A test class for an interface, this test class has two http requests, so should I write two MOCKs?

When I was writing a test class for an interface, I was told that the MOCK method needs to be used when the test class contains http requests. Introduction of MOCK Then I found that my interface needs to make two requests. The first time is to pass the account password to get the token, and then the second time I need to pass some content and the token and then return whether it is successful or not. Both requests use the same method, but the input parameters are different

Then I made two requests in the code, so do I need to write two MOCK classes, or do I need to write two

Test.setMock(HttpCalloutMock.class, new YourHttpCalloutMockImpl());


Solution 1:[1]

You can use the same mock class and return two different HttpResponse instances depending on the HTTPRequest parameters or url path

// within the YourHttpCalloutMockImpl.response method add something like this
if(/* any criteria based on the request params or url path */) {
  // return token response
} else {
  // return second request 
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sam.A.