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.setTimeout(30000);
request.setEndpoint('https://myAPI');
request.setMethod('POST');
request.setHeader( 'Authorization', 'Bearer ' + token );
request.setHeader('Content-Type', 'application/json');
request.setHeader('ClientId', clientId);
request.setBody(JSON.serialize(req));
System.debug('Request dump: ' + JSON.serialize(req));
// Make the call
HttpResponse response = http.send(request);
System.debug(response);
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:
@isTest
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');
res.setBody('{"example":"test"}');
res.setStatusCode(200);
return res;
}
}
This is my callout Test:
@isTest
public class GMSCalloutClassTest {
@isTest
static void testCallout(){
//String MockAccessToken = 'TestAccessToken';
//setmock callout class
Test.setMock(HttpCalloutMock.class, new GMSServiceDemoTest());
}
}