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();
  public String htmlBody {get{
    return generateReportData();

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

  public ReportDataComponentController(){
    reportId = '';
    htmlBody = '';
    reportWrapper = new Contact();
      reportId = ApexPages.currentPage().getParameters().get('reportId');            

  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.setHeader('Authorization','OAuth ' + UserInfo.getSessionID());

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

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

  global HttpResponse respond(HttpRequest request) {
    HttpResponse httpResponse = new HttpResponse();

    return httpResponse;


Test Class

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); 


        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();

  • It looks like you aren't creating any test data. – dphil May 19 '14 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 '14 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 '14 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 '14 at 5:31

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


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.

  • 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 '14 at 5:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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