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
farukh shaikhfarukh shaikh 

Hi Guys, I need a help here

I am trying to cover this generic method in Apex class,
public class apexClassGeneric{
 public static HttpResponse methodName(HttpRequest request) {
 HttpResponse response = h.send(request);
return response;

}
}

This is my test class.

public static testmethod void testAccountCallout() {
        SingleRequestMock fakeResponse = new SingleRequestMock(200,
                                                 'Complete',
                                                 '[{"Name": "sForceTest1"}]',
                                                 null);
        Test.setMock(HttpCalloutMock.class, fakeResponse);
        
        string endPoint='https://ap5.salesforce.com/services/apexrest/getAccountOnExternalIdtofetchsinglerecord/';
        HttpRequest req1=new HttpRequest();
        
        req1.setMethod('GET');
        req1.setEndpoint(endPoint);
        HttpResponse resp=apexClassGeneric.methodName(req1);
       
    }


I am not able to cover this I am getting error.

System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out

Note: If it would have been a method without HttpRequest request i would have cover it easily using,

HttpResponse resp=apexClassGeneric.methodName();
Khan AnasKhan Anas (Salesforce Developers) 
Hi Farukh,

Greetings to you!

You might be doing DML operations before web service callout so for resolving this issue you may call DML operation and web service callout in different test methods.

Please check this knowledge article to understand the problem:
https://help.salesforce.com/articleView?id=000326129&type=1&mode=1 (https://help.salesforce.com/articleView?id=000326129&type=1&mode=1)

Please refer to the below links which might help you further with the above issue.

https://salesforce.stackexchange.com/questions/228485/you-have-uncommitted-work-pending-please-commit-or-rollback-before-calling-out

https://salesforce.stackexchange.com/questions/115434/how-to-solve-callout-error-you-have-uncommitted-work-pending-please-commit-or

https://salesforce.stackexchange.com/questions/46438/unit-test-you-have-uncommitted-work-pending-please-commit-or-rollback-before-c

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
farukh shaikhfarukh shaikh

Hi Anas,

Thank you that you have a look on my code, I am pasting the complete code her. Could you please suggest me the necessary changes.
I tries changing some code but does not work.

public class CORE_IntegrationLogHandler {
   
    public static HttpResponse createIntegrationLog(HttpRequest request, string integrationProcessName) {

        Http h = new Http();
        HttpRequest req = request;
        HttpResponse response = h.send(req);
        Log__c log = new Log__c();
        if (req != null) {
            log.Request_Body__c = req.getBody();
            log.HTTP_Method__c = req.getMethod();
            log.Endpoint_URL__c = req.getEndpoint();
            log.Start__c = System.now();
            log.ProcessName__c = integrationProcessName;   
        }
        if (response != null) {
            
            log.Response_Body__c = response.getBody();
            log.HTTP_Status_Code__c = response.getStatusCode();
            log.End__c = System.now();
            
        }
        insert log;
        return response;

    }
 
    public static void webserviceResponseCatcher(RestRequest req,string jsonString,string integrationProcessName) {
        Log__c log = new Log__c();
        log.Start__c = System.now();
        log.End__c = System.now();
        log.HTTP_Method__c = req.httpMethod; // Getting HTTP request method.
        String serializedReqBody = JSON.serialize(req.params);
        log.Request_Body__c = serializedReqBody; //  Getting parameters that are received by the request.
        String serializedHeader = JSON.serialize(req.headers);
        log.Request_Header__c = serializedHeader;
        log.Response_Body__c=jsonString;
        log.ProcessName__c = integrationProcessName;
        insert log;

    }


}

This is my test class.
@isTest
public class CORE_IntegrationLogHandlerTest{

    @testSetup
    static void testDataSetup() {
        Account accObj = new Account(Name = 'Testing customer Account');
        insert accObj ;
    }

    public static testMethod void test_webserviceResponseCatcher() {
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();             
        req.requestURI = '/services/apexrest/Account/';
        req.httpMethod = 'POST';
        RestContext.request = req;
        RestContext.response= res;
        Account accObj = [ SELECT Id FROM Account LIMIT 1 ];
        string jsonString=JSON.serialize(accObj);
        string integrationProcessName='Test Integration';
        CORE_IntegrationLogHandler.webserviceResponseCatcher(req,jsonString,integrationProcessName);
       
    }
    
    public static testmethod void testAccountCallout() {
        SingleRequestMock fakeResponse = new SingleRequestMock(200,
                                                 'Complete',
                                                 '[{"Name": "sForceTest1"}]',
                                                 null);
        Test.setMock(HttpCalloutMock.class, fakeResponse);
        string integrationProcessName='Testing Integration';
        string endPoint='https://ap5.salesforce.com/services/apexrest/getAccountOnExternalIdtofetchsinglerecord/';
        string Jsonstring='{"Name":"Acc testname 1"}';
        HttpRequest req1=new HttpRequest();
        req1.setBody(Jsonstring);
        req1.setMethod('GET');
        req1.setEndpoint(endPoint);
        HttpResponse resp=CORE_IntegrationLogHandler.createIntegrationLog(req1,integrationProcessName);
       
    }
 }
 
farukh shaikhfarukh shaikh
Hi Anas,

I have Used ,

        Test.startTest();
        HttpResponse resp=CORE_IntegrationLogHandler.createIntegrationLog(req1,integrationProcessName);
        Test.StopTest();

and this working fine but is the correct way to resolve this issue? 
Leslie KingLeslie King
Hi,
 
I have a method in a controller that  is defined as static. I am writing a test class to test that method https://www.mykfcexperience.vip/
When I tried to call the method in the test class, it gave me the following error.
 
Error: Compile Error: Static methods cannot be invoked through an object instance: getRecentIdeasToday() at line 16 column 31
 
The method is defined in the controller as follows
public static integer getRecentIdeasToday(){
 
}