function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Alireza SeifiAlireza Seifi 

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 
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()

 
Best Answer chosen by Alireza Seifi
Lokeswara ReddyLokeswara Reddy
Usually this error can be seen if you try to invoke a static method from a non static method or vice-a-vice or mismatch in the number of parameters or data type.

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

Lokeswara ReddyLokeswara Reddy
Replace line# 11 and 12, in your test class with
HttpResponse resp = CheckbookAPI.getChecks(); 
Alireza SeifiAlireza Seifi
I tried this as well: I'm getting this error now : Error: Compile Error: Illegal assignment from String to System.HttpResponse at line 12 column 22
Lokeswara ReddyLokeswara Reddy
Then simply use the line as
CheckbookAPI.getChecks(); 
or
String response = CheckbookAPI.getChecks(); 
Alireza SeifiAlireza Seifi
:) Thank you.

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()
Lokeswara ReddyLokeswara Reddy
Usually this error can be seen if you try to invoke a static method from a non static method or vice-a-vice or mismatch in the number of parameters or data type.

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";
}



 
This was selected as the best answer