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
SFDC LearningSFDC Learning 

error in test class: System.JSONException: Malformed JSON: Expected '{' at the beginning of object

Here is my code: 

Global class SendAccountUsingRESTAPI {
  
      public String id;
      public String access_token;
      
     
  webservice static void RESTApiToAccount(id AccountId)
 
   {
      String clientId = 'xxxxx';
      String clientSecret = 'xxxx';
      String username = 'xxxxxx';
      String password = 'xxxxx';
    
     String reqbody = 'grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+username+'&password='+password;
   
     Http h = new Http();
      HttpRequest req = new HttpRequest();
      req.setBody(reqbody);
      req.setMethod('POST');
      req.setEndpoint('https://ap4.salesforce.com/services/oauth2/token');
      HttpResponse res = h.send(req);
     SendAccountUsingRESTAPI resp1 = (SendAccountUsingRESTAPI)JSON.deserialize(res.getbody(),SendAccountUsingRESTAPI.class);
     system.debug('@@@@access_token@@'+resp1 );
          
           String accessToken;
         
           accessToken = resp1.access_token;


           if(accessToken != null){
           String endPoint = 'https://ap4.salesforce.com/services/apexrest/Account';
           JSONGenerator gen = JSON.createGenerator(true);
          Account a=[select id,name,website from account where id=:AccountId];
           gen.writeStartObject();
           gen.writeStringField('name', a.name);
           gen.writeStringField('id', a.id);
           gen.writeStringField('website', a.website);
          
           String s=gen.getasString();
          
           Http h2 = new Http();
           HttpRequest req1 = new HttpRequest();
           req1.setHeader('Authorization','Bearer ' + accessToken);
           req1.setHeader('Content-Type','application/json');
           req1.setHeader('accept','application/json');
           

           req1.setBody(s);           
           req1.setMethod('POST');
           req1.setEndpoint(endPoint);
           HttpResponse res1 = h2.send(req1);
           String trimmedResponse = res1.getBody();
           system.debug('sasasasa'+res1);
           system.debug('@@@RESPONSE@@'+trimmedResponse);
           JSONParser parser = JSON.createParser(res1.getBody());
          
            
            
           
         }
   }
  
  
}


Here is my test classes: 

@isTest
public class MockHttpResponseGenerator implements HttpCalloutMock {
    // Implement this interface method
    
    public HTTPResponse respond(HTTPRequest req) {
    
    
             String endpoint = req.getEndpoint();


   
        if ( endpoint.contains('oauth2/token') ) {
            return buildOAuthResponse( req );
        } else if ( endpoint.contains('/services/apexrest/Account') ) {
            return buildShortenResponse( req );
        }

        return null;
    }

  private HttpResponse buildOAuthResponse( HttpRequest req ) {

        HttpResponse res = new HttpResponse();

        res.setBody('123');
        res.setStatusCode(200);

        return res;
    }

    private HttpResponse buildShortenResponse( HttpRequest req ) {

       HttpResponse res = new HttpResponse();
  
      //    JSONGenerator gen = JSON.createGenerator(true);
       //    gen.writeStartObject();
      //     gen.writeStringField('name','test');
     //      gen.writeStringField('id', '6546466');
    //       gen.writeStringField('website', 'vsbjs.com');
          
          String s=;
         res.setBody('{"Name": "sForceTest1"}]');
        res.setHeader('Content-Type', 'application/json');
        res.setStatusCode(200);
        

        return res;
   
    }
}


2nd test class: 

@isTest
private class CalloutClassTest {
  
  @isTest
    static void myTest() {        // Set mock callout class 
       
      Test.startTest();
 
      Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());

       Account a=new account();
       a.name='test';
       a.website='www.test.com';
     
       SendAccountUsingRESTAPI.RESTApiToAccount(a.id);
       Test.StopTest();
        
    }
}

Thank you in advance
Amit Chaudhary 8Amit Chaudhary 8
Can you try to update your mock class like below

@isTest
public class MockHttpResponseGenerator implements HttpCalloutMock {
    // Implement this interface method
    public HTTPResponse respond(HTTPRequest req) {
             String endpoint = req.getEndpoint();
        if ( endpoint.contains('oauth2/token') ) {
            return buildOAuthResponse( req );
        } else if ( endpoint.contains('/services/apexrest/Account') ) {
            return buildShortenResponse( req );
        }
        return null;
    }

  private HttpResponse buildOAuthResponse( HttpRequest req ) {
        HttpResponse res = new HttpResponse();
        res.setBody('123');
        res.setStatusCode(200);
        return res;
    }
    private HttpResponse buildShortenResponse( HttpRequest req ) {
       HttpResponse res = new HttpResponse();
      //    JSONGenerator gen = JSON.createGenerator(true);
       //    gen.writeStartObject();
      //     gen.writeStringField('name','test');
     //      gen.writeStringField('id', '6546466');
    //       gen.writeStringField('website', 'vsbjs.com');
          String s=;
         res.setBody('{"Name": "sForceTest1"}');
        res.setHeader('Content-Type', 'application/json');
        res.setStatusCode(200);
        return res;
    }
}

 
SFDC LearningSFDC Learning
I tried, but its giving the same error

Thank you