+ Start a Discussion
rupesh ranjanrupesh ranjan 

only 74 % code coverage how to increase? and ERROR : Error Message System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out Stack Trace Class.homeReport.getperformcallout:

Main Test Class

@isTest(seeAllData = false)
public class homeReportTests
{
   
  static testmethod void Myunittest1()
    {
       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.startTest();
       Test.setMock(HttpCalloutMock.class, new homepagemock());     
       homeReport testAObj = new homeReport();
       testAObj.getperformcallout();
      HttpResponse res = new HttpResponse();
      String actualValue = res.getBody();
        String expectedValue = '{"foo":"bar"}';
     System.assertEquals(actualValue, expectedValue);
       System.assertEquals(200, res.getStatusCode());
        cp.getperformcallout(); 
        Test.stopTest(); 
    } 
  
}
 
Mock Class

@IsTest(seeAllData = false)
global class homepagemock implements HttpCalloutMock 
{
    global HTTPResponse respond(HTTPRequest req) {   
       System.assertEquals('https://www.demomail.net/sf/api/HomeVideoViewReport?AUCode=00530000000rLMxAAM&SOrgID=00Dn00000008gwlEAA', req.getEndpoint());
       System.assertEquals('GET', req.getMethod());       
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        res.setBody('{"foo":"bar"}');
        res.setStatusCode(200);       
        return res;
    }  
}
 
Main Class 

public  class homeReport{

private ApexPages.StandardController c;
public homeReport(ApexPages.StandardController stdController) {
        c = stdController;
        }
        public  homeReport()
        {
        }
    String MySessionID = UserInfo.getSessionID();
     String myurl= URL.getSalesforceBaseUrl().toExternalForm() + '/services/Soap/c/10.0/' + UserInfo.getOrganizationId();
    public   string d='eU9WzoFgU4n8Apu5PYxcNGRZswRDZJWDEMdbQVU85gw=';
     public   String Acode= Userinfo.getuserid();
     public   String Oid = UserInfo.getOrganizationId();
    Blob cryptoKey= EncodingUtil.base64Decode(d);
    Blob data = Blob.valueOf(MySessionID );
    Blob data1 = Blob.valueOf(myurl);
    Blob data4 = Blob.valueOf(Acode);
    Blob data5 = Blob.valueOf(Oid);
    
  Blob encryptedData = Crypto.encryptWithManagedIV('AES256', cryptoKey, data);
    Blob encryptedData1 = Crypto.encryptWithManagedIV('AES256', cryptoKey, data1);
    Blob encryptedData4 = Crypto.encryptWithManagedIV('AES256', cryptoKey, data4 );
    Blob encryptedData5 = Crypto.encryptWithManagedIV('AES256', cryptoKey, data5 );
 
public     string b64Data = EncodingUtil.base64Encode(encryptedData);
 public       String b64Data1 = EncodingUtil.base64Encode(encryptedData1);
    public    String b64Data4 = EncodingUtil.base64Encode(encryptedData4);
    public    String b64Data5 = EncodingUtil.base64Encode(encryptedData5);
String testurl='sessionID='+EncodingUtil.urlEncode(b64Data, 'UTF-8')+'&serverUrl='+EncodingUtil.urlEncode(b64Data1, 'UTF-8');    
      
    public String aucode= EncodingUtil.urlEncode(b64Data4, 'UTF-8');
    public String ocode= EncodingUtil.urlEncode(b64Data5, 'UTF-8');


  public List<homeReportwrap> ConsoleWrapperList{get;set;}
  
       public List<homeReportwrap> getperformcallout(){
      ConsoleWrapperList = new List<homeReportwrap>();
       HttpRequest req = new HttpRequest(); 
       HttpResponse res = new HttpResponse();
        Http http = new Http(); 
        string url = 'https://www.demomail.net/sf/api/HomeVideoViewReport?AUCode='+aucode+'&SOrgID='+ocode+'&session='+testurl;     
      req.setEndpoint(url);
       req.setMethod('GET');
        res = http.send(req);
         if(res.getstatusCode() == 200 && res.getbody() != null)
          { 
        String replaceIllegal= res.getbody().replaceAll('\n','').replaceAll('\r','');
        ConsoleWrapperList=(List<homeReportwrap>)System.JSON.deserialize(replaceIllegal,List<homeReportwrap>.class);
          }
            return consolewrapperlist; 
             
          } 
 }

 
sandeep@Salesforcesandeep@Salesforce
Can you please go through with below refernece:
https://help.salesforce.com/apex/HTViewSolution?id=000003701