Proper way to test Remote Action which leverages helper class to perform a web callout

  • 1
  • Question
  • Updated 4 years ago
  • Answered
  • (Edited)
Another one for the hardcore folks:

I have a Remote Action in a controller extension class.  The Remote Action method invokes a static method on a helper class which then performs a web callout.  The Remote Action has no knowledge of the HTTP request/response ceremony in method it is calling nor should it.

Not quite sure of the proper way to test the Remote Action w/o setting up a HttpCalloutMock implementation.
// Arrange                
Test.setMock(HttpCalloutMock.class, new TRKD_MockHttpResponseGenerator());
// Act
String jsonReponse = MyController.doSomethingReturnJSONString();
// Assert
System.assert(jsonReponse != null);

It seems to me that I should not have to set up a HttpCalloutMock class since my Remote Action method has no knowledge of the MyController.doSomethingReturnJSONString method implementation i.e. code smell.

Do I change MyController.doSomethingReturnJSONString method to check for Test.IsRunningTest() and return an appropriate response?

Any advice would be appreciated.

Yes, the Remote Action is being invoked from a Skuid page. :)
Photo of Irvin Waldman

Irvin Waldman, Champion

  • 9,026 Points 5k badge 2x thumb

Posted 4 years ago

  • 1
Photo of Ben Hubbard

Ben Hubbard, Employee

  • 12,550 Points 10k badge 2x thumb
Hi Irvin,

Unfortunately, I can't think of anything better than the two methods you suggest. I think we typically branch off of Test.isRunningTest() and then provide an alternate response right there in the code. I agree that setting up a Mock for some internal method while testing an outer one smells a little off. :(
Photo of Irvin Waldman

Irvin Waldman, Champion

  • 9,026 Points 5k badge 2x thumb
Thank you, you confirmed my intuition and olfactics. :)  Thanks!