You need to sign in to do that
Don't have an account?

Callout Unit Test - Static methods cannot be invoked through an object instance: getChecks()
I'm new to Salesforce and I was wondering why do I get the following error for my code. if you can please write me back code would be greatfully aprreciated.
API class
API CALL class
TEST class
So I'm getting
API class
public class CheckbookAPI { // DigitalCheck__APIConfig__c is a custom setting object that stores the API Keys public static DigitalCheck__APIConfig__c Config = DigitalCheck__APIConfig__c.getOrgDefaults(); public CheckbookAPI() { } public static String getChecks() { HttpRequest req = CheckbookAPI.getHttpRequest('GET', '/transactions'); return CheckbookAPI.getHttpResponse(req); } public static String createCheck(String email, Decimal amount, String firstName, String lastName, String semail, String description) { HttpRequest req = CheckbookAPI.getHttpRequest('POST', '/send_check'); Map<String,Object> mapParams = new Map<String,Object>(); mapParams.put('email', email); mapParams.put('amount', amount); mapParams.put('first_name', firstName); mapParams.put('last_name', lastName); mapParams.put('sender_email', semail); mapParams.put('description', description); req.setBody(JSON.serialize(mapParams)); return CheckbookAPI.getHttpResponse(req); } private static String getHttpResponse(HttpRequest req) { Http http = new Http(); HTTPResponse response = http.send(req); return response.getBody(); } private static HttpRequest getHttpRequest(String Method, String Path) { // Initialize the request HttpRequest req = new HttpRequest(); // Build the selected elements String SelectedElements = ''; // Set the method req.setMethod(Method); SelectedElements += Method; // Set the Content-Type header if (Method == 'POST') { SelectedElements += 'application/json'; req.setHeader('Content-Type', 'application/json'); } // Set the endpoint String CompletePath = '/' + CheckbookAPI.Config.DigitalCheck__VersionAPI__c + Path; SelectedElements += CompletePath; req.setEndpoint(CheckbookAPI.Config.DigitalCheck__ServerURL__c + CompletePath); return req; } }
API CALL class
global with sharing class DigitalChecksChargeController { public DigitalChecksChargeController(ApexPages.StandardController stdController) { } @RemoteAction public static String processCharge() { return '200'; // Simulate the status code of the <charge> request } @RemoteAction public static String createCheck(String email, Decimal amount, String firstName, String lastName, String semail, String description) { return CheckbookAPI.createCheck(email, amount, firstName, lastName, semail, description); } }
TEST class
@isTest public class CheckbookAPI_UsingStaticResources { public testmethod static void testWS() { String testBody = 'This is a test :-)'; StaticResourceCalloutMock mock = new StaticResourceCalloutMock(); mock.setStaticResource('Test_WS'); mock.setStatusCode(200); Test.setMock(HttpCalloutMock.class, mock); CheckbookAPI callout = new CheckbookAPI (); HttpResponse resp = callout.getChecks(); // <== GETTING ERROR : Static methods cannot be invoked through an object instance: getChecks() //System.assertEquals(200, resp.getStatusCode()); //System.assertEquals(resp.getBody(), testBody); } }
So I'm getting
Static methods cannot be invoked through an object instance: getChecks()
If you see error something like Methods defined as TestMethod do not support Web service callouts
then you can modify your code line#25 (CheckbookAPI class)
if(!Test.isRunningTest()){
return CheckbookAPI.getHttpResponse(req);
}else{
return null;
// or return "dummy response";
}
All Answers
HttpResponse resp = CheckbookAPI.getChecks();
CheckbookAPI.getChecks();
or
String response = CheckbookAPI.getChecks();
Do you know why this keep hapening ? beacuase salesforce doesn't support callout?
Error: Compile Error: Method does not exist or incorrect signature: CheckbookAPI.createCheck()
If you see error something like Methods defined as TestMethod do not support Web service callouts
then you can modify your code line#25 (CheckbookAPI class)
if(!Test.isRunningTest()){
return CheckbookAPI.getHttpResponse(req);
}else{
return null;
// or return "dummy response";
}