I have an apex class that creates a custom field on an sObject via an HttpRequest and I'm not sure how to write a test for this because one of the things I need to test is that the field has the correct value, so a mock or a bypass won't work. I can't get enough code coverage for this class if I exclude the web service call. Not sure where to go with this.
This is the request portion of the class in question:
public class RollupFieldCreator {
@future(callout=true)
public static void createRollupField(String fieldName, String description) {
String fieldApiName = fieldName.trim().toLowerCase().replace(' ', '_') + '__c';
if (!Schema.SObjectType.Asset.fields.getMap().keySet().contains(fieldApiName)) {
String objectApiName = 'Asset';
String toolingApiEndpoint = '/services/data/v41.0/tooling/sobjects/CustomField/';
HttpRequest createField = new HttpRequest();
createField.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
createField.setHeader('Content-Type', 'application/json');
createField.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + toolingApiEndpoint);
createField.setMethod('POST');
String fieldDef = '{"Metadata" : {'
+ '"type" : "Text", "description" : "' + description
+ '", "inlineHelpText" : "", "precision" : null, "label" : "' + fieldName
+ '", "length" : 255, "required" : false}, '
+ '"FullName" : "' + objectApiName + '.' + fieldApiName + '"}';
createField.setBody(fieldDef);
Http httpInstance = new Http();
HttpResponse response = httpInstance.send(createField);
System.debug(response);
}