Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I'm working on two apex classes with use the HttpCalloutMock interface and they're working fine when I manually test, but I'm having trouble with the automated test class. Everything seems to look good, but I keep getting the following exception, any tips?

System.JSONException: no content to map to Object due to end of input

Class One

public class ReportDataComponentController{

  /*public String reportId{get;set;}
  public ReportWrapper__c reportWrapper {get{
    return reportWrapper();
  }set;}
  public String htmlBody {get{
    return generateReportData();
  }set;}*/

  public String reportId{get;set;}
  public Contact reportWrapper {get;set;}
  public String htmlBody {get;set;}

  public ReportDataComponentController(){
    reportId = '';
    htmlBody = '';
    reportWrapper = new Contact();
    if(ApexPages.currentPage().getParameters().containsKey('reportId')){
      reportId = ApexPages.currentPage().getParameters().get('reportId');            
    }
    populateReportWrapper();
    generateReportData();
  }

  Public void populateReportWrapper(){
    //ReportWrapper__c repWrapper = new ReportWrapper__c();
    for(Contact wrapper : [ Select id, name, email, firstname, lastname 
    from Contact
    where id = :reportId]){

      reportWrapper = wrapper; 

    }
    //return repWrapper;
  }

  public void generateReportData(){
    ReportCallout rptCallout = new ReportCallOut();
    String serverName = Apexpages.currentpage().getheaders().get('X-Salesforce-Forwarded-To');
    if(String.isBlank(serverName)) {
      serverName = 'prerelna1.pre.salesforce.com';
    }

    String json = rptCallout.getJSONFromReport(serverName, reportId);
    ReportParser reportParser = new ReportParser(json);

    htmlBody = reportParser.getHtmlTable();
    //return htmlExp;
  }
}

Class Two

global class ReportCallOut implements HttpCalloutMock {

  public String getJSONFromReport(String currentSFDCServerName,String reportID){
    //return getReportJSON();


    String baseURL = 'https://' + currentSFDCServerName +
                     + '/services/data/v29.0/analytics/reports/' + reportID + '?includeDetails=true';

    HttpRequest request = new HttpRequest();
    request.setMethod('GET');
    request.setEndPoint(baseURL);
    request.setHeader('Authorization','OAuth ' + UserInfo.getSessionID());
    request.setHeader('X-PrettyPrint','1');

    Http http = new Http();
    HttpResponse response = http.send(request);

    if(response.getStatusCode() == 200) {
        return response.getBody();
    }
    else
    {
        return null;
    }
  }

  global HttpResponse respond(HttpRequest request) {
    HttpResponse httpResponse = new HttpResponse();
    httpResponse.setStatusCode(200);
    //httpResponse.setBody(getReportJSON());

    return httpResponse;
  }

}

Test Class

@isTest
global class TestReportCallOutAndReportData{

    public TestReportCallOutAndReportData(){
    }

    static testMethod void TestReportCallAndReportData(){

        ReportCallOut rco = new ReportCallOut();
        String currentSFDCServerName;
        String reportID;
        HttpRequest hr = new HttpRequest();

        Test.setMock(HttpCalloutMock.class, new ReportCallOut());
        String srt = rco.getJSONFromReport(currentSFDCServerName,reportID);
        HttpResponse hrs = rco.respond(hr); 

        rco.getJSONFromReport(currentSFDCServerName,reportID);
        rco.respond(hr);

        String contentType = hrs.getHeader('Content-Type');
        String actualValue = hrs.getBody();
        String expectedValue = '{"foo":"bar"}';
        //System.assertEquals(actualValue, expectedValue);
        //System.assertEquals(200, hrs.getStatusCode());                 

        ReportDataComponentController rdcc = new ReportDataComponentController();
        rdcc.populateReportWrapper();
        rdcc.generateReportData();  


    }
}
share|improve this question
    
It looks like you aren't creating any test data. –  dphil May 19 at 13:35
    
Thanks for reply, but i am not getting System.JSONException error and facing this issue first time so can you please write test class for this code ? –  user1969206 May 20 at 5:27
    
Besides your exception, You shouldn't be trying to do a call out in your mock service .. that defeats the whole point of mocking a call out. Try updating your test code and feel free to update your question when you get stuck. (have a look at : salesforce.stackexchange.com/questions/32374/… ) –  Samuel De Rycke May 20 at 14:06
    
I have tried everything but didn't get anything please can you give me perfect place where i can change my test class code ? –  user1969206 May 21 at 5:31
add comment

1 Answer 1

You have to hard code the expected response in mock test class:

httpResponse.setBody('{"foo":"bar"}');

also make sure that the response must be a valid json.

The best way is copy the real result from system debug log and set it as response body in mock test class.

share|improve this answer
    
Thanks for reply but this is not helpful for me. I have hard coded response code. HttpResponse res = new HttpResponse(); res.setBody('{"foo":"bar"}'); and then i write my class ReportDataComponentController rdcc = new ReportDataComponentController(); rdcc.populateReportWrapper(); rdcc.generateReportData(); still facing same issue. Please Can you paste code of test class of my 2 classes ? –  user1969206 May 20 at 5:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.