I've been scouring all the documentation, trailhead, and here and I can't get this to work. I'm running into a wall in test class where I'm using the HttpCalloutMock method, but I can't return the response because my class that I am calling returns void and I'm getting "Illegal assignment from void to System.HttpResponse". I can't make it not void because it is a @future callout. Is there just another place I need to be looking? I can post code if needed but I think this is more of a general question of testing @future callouts.
Edit:
Posting my code below.
Here is the class I am trying to get code coverage on (currently 0% despite everything I've tried)
public class PortalQuery {
@future (callout=true)
public static void portalGet(String match_id){
//set Match Record
Match_Record__c mr = [SELECT Id, PODs__c
FROM Match_Record__c
WHERE Match_ID__c = :match_id];
System.debug(match_id);
System.debug(mr.Id);
//callout to Portal API
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint('url here');
req.setMethod('GET');
HttpResponse res = http.send(req);
// If the request is successful, parse the JSON response.
if (res.getStatusCode() == 200) {
system.debug('response = ' + res.getBody());
String jsonContent = res.getBody();
System.debug(jsonContent);
//call JSON Utility to help with parse
Util_JSONParser parser = Util_JSONParser.createParser(jsonContent);
//parse out Match Record fields for updates
String match_algorithm_version = parser.get('match_algorithm_version').Value;
if(match_algorithm_version != NULL){
Decimal mav = Decimal.valueOf(match_algorithm_version);
mr.Match_Algorithm_Version_Number__c = mav;
}
}
mr.Last_Match_API_Update__c = System.now();
update mr;
}
}
RestResource that calls to class:
@RestResource(urlMapping='/mrUpdate')
global with sharing class mrUpdate {
@HTTPPatch
//query SFDC Match Record from portal match_id
global static String updateMR(String match_id, String token){
Match_Record__c mr = [SELECT Id, Send_POD__c
FROM Match_Record__c
WHERE Match_ID__c = :match_id];
System.debug('***Here is the MATCH ID*** ' + match_id);
System.debug('!!!!Here is the TOKEN!!!!! ' + token);
//call portal for MR fields
if (token != 'some token'){
PortalQuery.portalGet(match_id);
}
//call portal for PODs
if (mr.Send_POD__c == TRUE){
PortalQuery.podQuery(match_id);
}
return null;
//call response method to show udpated Match Record json
List <Match_Record__c> updatedMR = mrResponse.getUpdatedMR();
return null;
}
}
And the test class
@isTest
public class portalQueryTest {
@isTest
static void setData(){
Match_Record__c mr = new Match_Record__c();
List <Match_Record__c> mrList = new List <Match_Record__c>();
String match_id = 'name';
String token = 'token';
Account account = new Account();
account.Name = 'Account';
insert account;
Contact contact = new Contact();
contact.FirstName = 'Contact';
contact.LastName = 'Jones';
contact.AccountId = account.Id;
insert contact;
Guardant_360_Sample__c sample = new Guardant_360_Sample__c();
sample.Name = 'name';
sample.Contact__c = contact.Id;
sample.Account__c = account.Id;
insert sample;
mr.Name = 'name';
mr.Match_ID__c = 'name';
mr.Portal_Sample_ID__c = 'sample';
mr.Last_Match_API_Update__c = System.now();
insert mr;
mrList.add(mr);
POD__c pod = new POD__c();
pod.Match_Record__c = mr.Id;
insert pod;
Test.startTest();
//I am honestly totally lost at this point.
//I've looked at so many pages I'm not sure what to do here
//to get a json string into the class I'm trying to test.
//I've tried the mockcallout, using a static resource, etc....
Test.stopTest();
}
}
PortalQuery.portalGet
method invoked from? Is it called from a trigger when inserting aMatch_Record__c
record?mr.Last_Match_API_Update__c = System.now();
so to validate it worked, after stop test query formr.Last_Match_API_Update__c = System.now();
and assert that it was populated (assuming it was null before) - But first you need to set up your mock implementation as I do not see it in your test class