I am trying to write a test class for a @AuraEnabled Apex Class. But I can not get it to pass. The first thing is you can not return an HTTPRequest or Response to a @AuraEnabled Class so I can not match the types from the Test to the Main Class. Any Help getting this code to pass would be greatly appreciated.
Main Class
public class APICaller{
@AuraEnabled
public static String CallApi(){
HttpRequest req = new HttpRequest();
req.setEndpoint({MY-URL-ENDPOINT});
req.setMethod('POST');
req.setHeader('Content-Type', 'application/json');
req.setTimeout(120000);
String body = '{"Username":"username"}';
req.setBody(body);
Http http = new Http();
HTTPResponse res = http.send(req);
return res.getBody();
}
}
Test Class
@isTest
public class KonnectidAlpha_GetClients_Test{
@isTest public static void CallTestApi(){
// Associate the callout with a mock response
Test.setMock(HttpCalloutMock.class, new HttpCalloutMockClass());
// Call method to test
String response = APICaller.CallApi();
System.debug(response);
String actualValue = response;
String expectedValue = '{"response": "Records Synced"}';
System.assertEquals(actualValue, expectedValue);
}
}
HTTPCalloutMock Class
@isTest
global class HttpCalloutMockClass implements HttpCalloutMock {
// Implement this interface method
global HTTPResponse respond(HTTPRequest request) {
// Create a fake response
HttpResponse response = new HttpResponse();
response.setHeader('Content-Type', 'application/json');
String body = '{"response": "Records Synced"}';
response.setBody(body);
response.setStatusCode(200);
return response;
}
}