Skip to content

Instantly share code, notes, and snippets.

@abhinavguptas
Created September 24, 2011 03:05
Show Gist options
  • Save abhinavguptas/1238904 to your computer and use it in GitHub Desktop.
Save abhinavguptas/1238904 to your computer and use it in GitHub Desktop.
Fixture for Apex HTTP Callout Testing [Note: file name kept .java instead of .cls for syntax highlighting :)]
/**
Collection of classes/interfaces for creating a fixture that will ease the Testing effort with webservices.
*/
public with sharing class WS {
/**
Contract for HTTPResponse. To avoid learning and confusions this interface is exposing
all the methods available in Apex HTTPResponse class(http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_http_httpresponse.htm)
Test classes can override and implement them as required
*/
public interface IHttpResponse {
String getBody();
Dom.Document getBodyDocument();
String getHeader(String key);
String[] getHeaderKeys();
String getStatus();
Integer getStatusCode();
Xmlstreamreader getXmlStreamReader();
// have to name it toStrings() instead of toString(), as the later
// is reserved by Apex
String toStrings();
}
/**
Indicator for operation being accessed is virtual as of now.
*/
public class VirtualOperationException extends Exception {}
/**
Meant to be base/parent class for Apex test case simulations.
Its a mock implementation of IHttpResponse, it gives a virtual body
of all contract methods in IHttpResponse, and throws VirtualOperationException
for every method call. Using this class as parent, subclasses would be easy i.e. just
override the methods required, instead of implementing the whole IHttpResponse contract.
For ex. in most of the cases, one will override getStatusCode() and getBody() for testing purposes.
*/
public virtual class MockHttpResponseBase implements IHttpResponse {
public virtual String getBody() {
throw new VirtualOperationException('No implementation available !');
}
public Dom.Document getBodyDocument() {
throw new VirtualOperationException('No implementation available !');
}
public virtual String getHeader(String key) {
throw new VirtualOperationException('No implementation available !');
}
public virtual String[] getHeaderKeys() {
throw new VirtualOperationException('No implementation available !');
}
public virtual String getStatus() {
throw new VirtualOperationException('No implementation available !');
}
public virtual Integer getStatusCode() {
throw new VirtualOperationException('No implementation available !');
}
public virtual Xmlstreamreader getXmlStreamReader() {
throw new VirtualOperationException('No implementation available !');
}
public virtual String toStrings() {
throw new VirtualOperationException('No implementation available !');
}
}
/**
Default wrapper implementation over standard Apex HttpResponse class.
Reference : http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_http_httpresponse.htm
All contract methods of IHttpResponse are delegated to the wrapped Apex HttpResponse instance.
*/
public virtual class DefHttpResponse implements IHttpResponse {
public Httpresponse resp;
public DefHttpResponse(HttpResponse resp) {
this.resp = resp;
}
public String getBody() {
return resp.getBody();
}
public Dom.Document getBodyDocument() {
return resp.getBodyDocument();
}
public String getHeader(String key) {
return resp.getHeader(key);
}
public String[] getHeaderKeys() {
return resp.getHeaderKeys();
}
public String getStatus() {
return resp.getStatus();
}
public Integer getStatusCode() {
return resp.getStatusCode();
}
public Xmlstreamreader getXmlStreamReader() {
return resp.getXmlStreamReader();
}
public String toStrings() {
return resp.toString();
}
}
/**
Contract for a simple web service callout using Apex.
Only a single method is available for abstracting the stuff out for ease of Testing.
Test classes can provide implmentations of this interface to return custom/fake/mock HTTP responses.
*/
public interface IHttpCallout {
/**
Accepts a ready to send requests and makes a callout using that.
*/
IHttpResponse send(HttpRequest req);
}
/**
Default implementation meant for use in actual apex code. It runs out of
standard Apex Http Class (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_http_http.htm)
*/
public class DefHttpCallout implements IHttpCallout {
public IHttpResponse send(HttpRequest req) {
Http http = new Http();
return new DefHttpResponse(http.send(req));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment