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
KR_ForceKR_Force 

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

Class.TestEvalEndDateHTTPCallOut.testEVAlEndDateCallout: line 28, column 1
  1. @isTest
  2. public class TestEvalEndDateHTTPCallOut {
  3.     public static testmethod void testEVAlEndDateCallout() {
  4.         SingleRequestMock fakeResponse = new SingleRequestMock(200,
  5.                                                  'Complete',
  6.                                                  '[{"Name": "sForceTest1"}]',
  7.                                                  null);
  8.         Test.setMock(HttpCalloutMock.class, fakeResponse);
  9.         set<ID> ast=new Set<ID>();
  10.         Account a = new Account();
  11.         a.Name = 'test account';
  12.         a.Product_Subscriptions__c='test;test:';
  13.         insert a;
  14.         Asset a1 = new Asset(Name='testAsset', RVBD_Product_Family__c= 'test',IB_Status__c='Under Evaluation',Instance_Number__c='77777777XX',AccountID=a.id);
  15.         insert a1;
  16.         ast.add(a1.id);
  17.        
  18.         Contact foo = new Contact(FirstName = 'Foo', LastName='Bar');
  19.         insert foo;
  20.         Case c = new Case(Subject = 'Test case', ContactId=foo.id,AssetID=a1.id,AccountID=a.id);
  21.         Insert c;
  22.        
  23.         PageReference pageRef = Page.RefreshEvalEndDate;
  24.         Test.setCurrentPage(pageRef);
  25.         Apexpages.StandardController stdController = new Apexpages.StandardController(a1);
  26.         Apexpages.currentPage().getParameters().put('id',a1.Id);
  27.         EvalEndDateHTTPCallOut controller = new EvalEndDateHTTPCallOut(StdController);
  28.         controller.parseJSONResponse();
  29.         EvalEndDateHTTPCallOut.parseJSONResponseFuture(ast);
  30.       
  31.     }
  32. }
AshlekhAshlekh
Hi,

This error occur when you are inserting any record before any callout. You have to callout first then call any DML operation.


KR_ForceKR_Force
Hi Ashlekh Gera (D-Horse),

To make the call out i need to pass the asset record to main method, with out test being created how can i make a call out?

if undertood wrong can you please share some sample code which helps me to build my test class?

thanks for your help!
AshlekhAshlekh
Hi,

I think Ashlekh has deleted his post.

See below code 
@isTest
public class TestEvalEndDateHTTPCallOut {
    public static testmethod void testEVAlEndDateCallout() {
        SingleRequestMock fakeResponse = new SingleRequestMock(200,
                                                 'Complete',
                                                 '[{"Name": "sForceTest1"}]',
                                                 null);
        Test.setMock(HttpCalloutMock.class, fakeResponse);
        set<ID> ast=new Set<ID>();
        Account a = new Account();
        a.Name = 'test account';
        a.Product_Subscriptions__c='test;test:';
        insert a;
        Asset a1 = new Asset(Name='testAsset', RVBD_Product_Family__c= 'test',IB_Status__c='Under Evaluation',Instance_Number__c='77777777XX',AccountID=a.id);
        insert a1;
        ast.add(a1.id);
       
        Contact foo = new Contact(FirstName = 'Foo', LastName='Bar');
        insert foo;
        Case c = new Case(Subject = 'Test case', ContactId=foo.id,AssetID=a1.id,AccountID=a.id);
        Insert c;
       
        PageReference pageRef = Page.RefreshEvalEndDate;
        Test.setCurrentPage(pageRef);
        Apexpages.StandardController stdController = new Apexpages.StandardController(a1);
        Apexpages.currentPage().getParameters().put('id',a1.Id);
Test.startTest();
        EvalEndDateHTTPCallOut controller = new EvalEndDateHTTPCallOut(StdController);
        controller.parseJSONResponse();
        EvalEndDateHTTPCallOut.parseJSONResponseFuture(ast);
      Test.stopTest();
    }
}



KR_ForceKR_Force
Hi,

Starttest and stoptest did help to fix callout exception but now running into "System.TypeException: Invalid conversion from runtime type LIST&lt;ANY&gt; to MAP&lt;String,ANY&gt;" error at line 31. any suggestions?
  1. @isTest
  2. public class TestEvalEndDateHTTPCallOut {
  3.     public static testmethod void testEVAlEndDateCallout() {
  4.        SingleRequestMock fakeResponse = new SingleRequestMock(200,
  5.                                                  'Complete',
  6.                                                  '[{"Name": "sForceTest1"}]',
  7.                                                  null);
  8.                                                 
  9.        // List<MockResponse> responseList = (List<MockResponse>)JSON.deserialize(res.getBody(), SingleRequestMock.class);
  10.         Test.setMock(HttpCalloutMock.class, fakeResponse );
  11.         set<ID> ast=new Set<ID>();
  12.         Account a = new Account();
  13.         a.Name = 'test account';
  14.         a.Product_Subscriptions__c='test;test:';
  15.         insert a;
  16.         Asset a1 = new Asset(Name='testAsset', RVBD_Product_Family__c= 'test',IB_Status__c='Under Evaluation',Instance_Number__c='77777777XX',AccountID=a.id);
  17.         insert a1;
  18.         ast.add(a1.id);
  19.        
  20.         Contact foo = new Contact(FirstName = 'Foo', LastName='Bar');
  21.         insert foo;
  22.         Case c = new Case(Subject = 'Test case', ContactId=foo.id,AssetID=a1.id,AccountID=a.id);
  23.         Insert c;
  24.        
  25.         PageReference pageRef = Page.RefreshEvalEndDate;
  26.         Test.setCurrentPage(pageRef);
  27.         Apexpages.StandardController stdController = new Apexpages.StandardController(a1);
  28.         Apexpages.currentPage().getParameters().put('id',a1.Id);
  29.         Test.startTest();
  30.         EvalEndDateHTTPCallOut controller = new EvalEndDateHTTPCallOut(StdController);
  31.         controller.parseJSONResponse();
  32.         EvalEndDateHTTPCallOut.parseJSONResponseFuture(ast);
  33.         Test.stopTest();
  34.     }
  35. }