0

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();
    }
}
5
  • Why would you care about the return value? I think seeing your code might be instructive.
    – Adrian Larson
    Mar 30, 2017 at 19:15
  • Maybe I'm not needing it at all? I'm just trying to get the class that calls out to receive a json string and parse it out as needed so I can get some code coverage. Mar 30, 2017 at 20:46
  • Where is your PortalQuery.portalGet method invoked from? Is it called from a trigger when inserting a Match_Record__c record?
    – dsharrison
    Mar 30, 2017 at 20:55
  • @D.S. ah, it's being called via a rest resource, I was trying to get to it directly, that's getting me closer. Copied the rest resource above. So now I'm just needing the json response. Mar 30, 2017 at 21:15
  • @hippiecoder - Your future method updates the mr.Last_Match_API_Update__c = System.now(); so to validate it worked, after stop test query for mr.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
    – Eric
    Mar 31, 2017 at 3:12

1 Answer 1

2

Future methods are asynchronous so they cannot return a value (hence the void return type). Instead you need to do something in the future method with your HTTP response (like put it in the database). In your test, calling Test.stopTest() will ensure that the @future method completes. Then, you can query for the result you updated in the @future method to ensure the update took place successfully.

3
  • Ah! That makes total sense. Thank you, I'll mess with that then respond. Mar 30, 2017 at 19:21
  • Hmm, not sure how to make that happen. Maybe you are meaning have a line in there about a system.assert? I can modify the code around (and did) but still wasn't able to get any code coverage on this callout class. Mar 30, 2017 at 20:52
  • There's a pretty good example in the Apex Developer Guide Mar 30, 2017 at 23:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .