0

I have a requirement to call a external service with post method.

When i tested in anymonous block.It's working fine.

But when i try to set up test classes i got this error.

in line

Map results = (Map) JSON.deserializeUntyped(res.getBody());

            HttpRequest req = new HttpRequest();
    req.setEndpoint('https://api.incontact.com/InContactAuthorizationServer/Token');
            req.setMethod('POST');

            // Specify the required user name and password to access the endpoint
            // As well as the header and header information

            String username = 'sfintegration';
            String password = 'Test';
            String vendor   = 'Salesforce';
            String businessunit ='459';
            String application ='Callout';
            Blob headerValue = Blob.valueOf(application + '@' + vendor + ':' + 
             businessunit);
            String authorizationHeader = 'BASIC ' +
            EncodingUtil.base64Encode(headerValue);
            req.setHeader('Authorization', authorizationHeader);
            //req.setBody(str);
            // Create a new http object to send the request object
            // A response object is generated as a result of the request

            req.setHeader('Content-Type', 'application/json;charset=UTF-8');
            JSONGenerator gen = JSON.createGenerator(true);    
            gen.writeStartObject();      
            gen.writeStringField('grant_type','password');
            gen.writeStringField('username',username);
            gen.writeStringField('password',password);
            gen.writeStringField('scope','');
            gen.writeEndObject();
            String jsonS = gen.getAsString();
            system.debug('Data in gen'+jsonS);
            req.setBody(jsonS);

            // Create a new http object to send the request object
            // A response object is generated as a result of the request  

            Http http = new Http();
            HTTPResponse res = http.send(req);
            System.debug(res.getBody());
            System.debug(res.getStatusCode());

            //JSONParser parser = JSON.createParser(res.getBody());

            Map<String,Object> results = (Map<String,Object>) 
            JSON.deserializeUntyped(res.getBody());

            //System.debug('Values in results: '+results.values());
            //System.debug('Keys in list: '+results.keyset());

            accesstoken=String.valueof(results.get('access_token'));
            baseurl=String.valueof(results.get('resource_server_base_uri'));
            System.debug('Access Token:'+accesstoken);
            System.debug('baseurl: '+baseurl);
            If(accesstoken !=null && baseurl !=null)
            {
                for(Lead ts:TriggerLeads)
                {
                    String s=String.valueOf(ts.Phone);
                    RequestACallback(accesstoken,baseurl,s);
                }
            }

Please let me know where i missed it.

3
  • Share your test class as well. It looks like your mock response didn't setup correctly. Commented Aug 14, 2018 at 12:48
  • Good catch! Response body in my mock class is not properly written. Please post your answer i will close the question.
    – Pavan tej
    Commented Aug 14, 2018 at 12:57
  • Without including your test code, this question is low quality and unlikely to help anyone else.
    – Adrian Larson
    Commented Aug 14, 2018 at 14:11

1 Answer 1

1

It looks like your mock response didn't setup correctly. You need to setup it correctly.

Do that and it will solve your problem.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .