You need to sign in to do that
Don't have an account?
William Roach-Barrette
Deployment Failed: How to work around "Methods defined as TestMethod do not support Web service callouts Stack Trace: null"
I want to begin by saying I am very new to salesforce and deploying code on salesforce, so this question might be very rudimentary at its core. That being said, I need assistance creating a test class for some code I have developed. This code reachs out to an external server to pull a JSON file that contains some user prefrences I want to import into salesforce. I have all my code debugged and moved over to my production instance, but no matter what I do I keep getting this as an error when I try to validate:
And here is my code:
Wrapper:
JSON Parser:
My main question is this: How do I modify my desearialize method so that I can make use of a static resource for testing purposes?
Methods defined as TestMethod do not support Web service callouts Stack Trace: nullI know I need some kind of test class, and through my research I tihnk I can make use of Static Resources to offer salesforce a way to test my code without actually preforming a web service callout. I just dont know how best to proceed. If I upload some sample JSON code into a static resource then make a test class that tests my code, how do I tell salesforce to use the static resource over the actual web service callout? Here is a link to what I plan on using as a recource: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_static.htm
And here is my code:
Wrapper:
public class GDPRWrapper{ public GDPRWrapper(List<GDPRData> templst){ GDPRList = templst; } public List<GDPRData> GDPRList {get; set;} public class GDPRData { public Integer gdprId {get; set;} //26636 public String firstName {get; set;} public String lastName {get; set;} public String email {get; set;} public String phone {get; set;} public String accountName {get; set;} public String contactId {get; set;} //AA111222333AAAe public String emailHash {get; set;} //78fcb5ad502033c46d35abcecb3615bd92757fb0451485a19b27b7515f6d82d0 public String createDate {get; set;} //2018-05-17T15:19:37.000+0000 public String responseDate {get; set;} //2018-05-21T10:38:53.000+0000 public String notifyDate {get; set;} public boolean marketing {get; set;} public boolean security {get; set;} public boolean support {get; set;} public boolean contactPhone {get; set;} public boolean contactEmail {get; set;} public boolean contactOther {get; set;} public boolean invalid {get; set;} } public static List<GDPRData> parse(httpResponse json){ return (List<GDPRData>) System.JSON.deserialize(json.getBody(), List<GDPRData>.class); } }
JSON Parser:
public class JSONDeserialize { public GDPRWrapper wrapper {get;set;} @Future(callout=true) public static void deserialize() { GDPRWrapper wrapper; Http h = new Http(); HttpRequest request = new HttpRequest(); request.setEndPoint('**********************'); Blob headerValue = Blob.valueOf('d18849ea4155:d83ce6ef3dbe'); String authorizationHeader = ('Basic ' + EncodingUtil.base64Encode(headerValue)); request.setHeader('Authorization', authorizationHeader); request.setMethod('GET'); try{ HttpResponse response = h.send(request); System.debug('JSON RESPONSE: ' + response); List<GDPRWrapper.GDPRData> obj = GDPRWrapper.parse(response); wrapper = new GDPRWrapper(obj); System.assert(wrapper.GDPRList!=null); updateData(wrapper); }catch(JSONException j){ System.debug('An unexpected error has occured: ' + j.getMessage()); } } public static void UpdateData(GDPRWrapper wrapper){ List<Contact> contactPref = new List<Contact>(); List<Contact> newContacts = new List<Contact>(); for(Integer i = 0; i < wrapper.GDPRList.size(); i ++){ if(wrapper.GDPRList[i].contactId.length() > 3){ if(wrapper.GDPRList[i].contactId.subString(0,3) == '003'){ Contact toInsert = new Contact(); toInsert.firstName = wrapper.GDPRList[i].firstName; toInsert.lastName = wrapper.GDPRList[i].lastName; toInsert.email = wrapper.GDPRList[i].email; toInsert.Email_Hash__c = wrapper.GDPRList[i].emailHash; toInsert.Id = wrapper.GDPRList[i].contactId; toInsert.Sales_and_Marketing__c = wrapper.GDPRList[i].marketing; toInsert.Critical_Security_Notes__c = wrapper.GDPRList[i].security; toInsert.Product_Information__c = wrapper.GDPRList[i].support; toInsert.Contact_Via_Text__c = wrapper.GDPRList[i].contactPhone; toInsert.Contact_Via_Email__c = wrapper.GDPRList[i].contactEmail; contactPref.add(toInsert); } } else{ Contact toInsert = new Contact(); toInsert.firstName = wrapper.GDPRList[i].firstName; toInsert.lastName = wrapper.GDPRList[i].lastName; toInsert.email = wrapper.GDPRList[i].email; toInsert.Email_Hash__c = wrapper.GDPRList[i].emailHash; toInsert.Sales_and_Marketing__c = wrapper.GDPRList[i].marketing; toInsert.Critical_Security_Notes__c = wrapper.GDPRList[i].security; toInsert.Product_Information__c = wrapper.GDPRList[i].support; toInsert.Contact_Via_Text__c = wrapper.GDPRList[i].contactPhone; toInsert.Contact_Via_Email__c = wrapper.GDPRList[i].contactEmail; newContacts.add(toInsert); } } try{ if(contactPref.size()>0){ upsert contactPref; } if(NewContacts.size()>0){ insert NewContacts; } } catch(DmlException e){ System.debug('An unexpected error has occured: ' + e.getMessage()); } } }
My main question is this: How do I modify my desearialize method so that I can make use of a static resource for testing purposes?