+ Start a Discussion
rupesh ranjanrupesh ranjan 

Error in RUN Test Class(You have uncommitted work pending. Please commit or rollback before calling out)

Error Message    System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out
Stack Trace    Class.homeReport.getperformcallout: line 55, column 1
Class.homeReportTests.Myunittest1: line 33, column 1
@isTest(seeAllData = false)
public class homeReportTests
{
    public List<homeReportwrap> ConsoleWrapperList{get;set;}
  static testmethod void Myunittest1()
    {
      User a = new User();
       a.FirstName = 'rup';
       a.LastName  = 'ran';
       a.Email     = 'rup.ran@gmail.com';
       a.Username  = 'rup1.ran1@gmail.com';
       a.Alias     = 'rupran';
       a.ProfileId = '00e30000000mm8h';
       // Don't worry about these
       a.TimeZoneSidKey    = 'America/Denver';
       a.LocaleSidKey      = 'en_US';
       a.EmailEncodingKey  = 'UTF-8';
       a.LanguageLocaleKey = 'en_US';
       insert a;
       Account account = new Account();
       account.Name = 'Accc';
       insert account;   
       Contact contact = new Contact();
       contact.FirstName = 'rup';
       contact.LastName = 'ran';
       contact.AccountID = account.Id;
       contact.Email = 'rup.ran@gmail.com';
       contact.MobilePhone = '918527116725';
       insert contact;  
       homeReport cp = new homeReport(new ApexPages.StandardController(contact));
       Test.setMock(HttpCalloutMock.class, new homepagemock());
       homeReport testAObj = new homeReport();
       testAObj.getperformcallout();
      HttpResponse res = new HttpResponse();
  
    //String contentType = res.setHeader('Content-Type');
   // System.assert(contentType == 'application/json');
      String actualValue = res.getBody();
     String expectedValue = '{"foo":"bar"}';
     System.assertEquals(actualValue, expectedValue);
       System.assertEquals(200, res.getStatusCode());
       cp.getperformcallout(); 
        
    }
    
   
   
/*  static testMethod void fakeTest(){
  

     homeReport.fakeMethod();
   

     //repeat the same for the rest of classes.
  } */
  
 /*  static testMethod void fakeTest1(){
   
  } */
  
  
}

 
sandeep@Salesforcesandeep@Salesforce
Sometimes a developer needs to create a record and then update it with information provided by a Web Service. However, a Web Service Callout may not occur after a DML statement within the same transaction. To achieve the required action, the transaction must be separated into two parts so that the DML transaction is completed before the Web Service Callout occurs.

Can you please check this in your apex class for which you are wrtting test class or you can provide code to me so that I Can find it out.

Thanks
Sandeep Singhal
http://www.codespokes.com/