I have used some of the guides available like (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm), but still having trouble completing the test class. I am not exactly sure how I can call "doAdvancedSearch". This is what I have:

public with sharing class GMSServiceDemo {

    private static String clientId ='4323-4323'; 

    public static List<GMSDonorSearchModel.GMSDonorOutputSearchResult> doAdvancedSearch(GMSDonorSearchModel.GMSDonorInputSearch req){

        List<GMSDonorSearchModel.GMSDonorOutputSearchResult> results = new List<GMSDonorSearchModel.GMSDonorOutputSearchResult>();

        // Get token 
        String token = doLogin();

        // Make the service call
        Http http = new Http();
        HttpRequest request = new HttpRequest();

        request.setHeader( 'Authorization', 'Bearer ' + token );
        request.setHeader('Content-Type', 'application/json');
        request.setHeader('ClientId', clientId);
        System.debug('Request dump: ' + JSON.serialize(req));

        // Make the call
        HttpResponse response = http.send(request);
        if( response.getStatusCode()==200){
            if (!response.getBody().contains('No data found')){ 
                System.debug('Adv Search Response: ' + response.getBody());

                results = (List<GMSDonorSearchModel.GMSDonorOutputSearchResult>)JSON.deserialize(response.getBody(), List<GMSDonorSearchModel.GMSDonorOutputSearchResult>.class);

            } else if (response.getBody().contains('No data found')){
                System.debug('No Data Found: ' + response.getStatusCode() + ': ' + response.getStatus());
        else {
            System.debug('doAdvanceSearch returned the following status code: ' + response.getStatusCode() + ': ' + response.getStatus());
        return results;    

This is my MockTest:

global class GMSServiceDemoTest implements HttpCalloutMock {

    // Implement this interface method      
    global HTTPResponse respond(HTTPRequest req) {

        System.assertEquals('https:myAPI, req.getEndpoint());
        System.assertEquals('POST', req.getMethod());

        //create fake response
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        return res;

This is my callout Test:

public class GMSCalloutClassTest {

    static void testCallout(){
      //String  MockAccessToken = 'TestAccessToken';
        //setmock callout class
        Test.setMock(HttpCalloutMock.class, new GMSServiceDemoTest()); 

  • 1
    GMSServiceDemo. doAdvancedSearch(GMSDonorSearchModel.GMSDonorInputSearch req); right underneath your test.setMock – Ryan Sherry Jul 8 '19 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.