Taking into consideration the below pseudo code, do we need to check if an apex test is running before performing callout? Or does the Test.setMock() method automatically prevent callouts during a test run?
static void performCallout(){
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod(method);
req.setBody(body);
Http http = new Http();
//Do I need to check if test is running here by using
//Test.isRunningTest() so that I do not receive error during testing?
HttpResponse res = http.send(req);
}
Test.Start();
Test.setMock(HttpCalloutMock() mock, <class utilizing HttpCalloutMock interface>);
//call method that performs callout to external service
performCallout();
Test.Stop();
This documentation seems to imply that by using Test.setMock() inside your test class you do not need to check if a test is running before performing a callout because the method will "send a mock response whenever a callout is made through the HTTP classes." See below for relevant portion:
Sets the response mock mode and instructs the Apex runtime to send a mock response whenever a callout is made through the HTTP classes or the auto-generated code from WSDLs.