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.

  • Share your test class as well. It looks like your mock response didn't setup correctly. – Tushar Sharma Aug 14 '18 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 Aug 14 '18 at 12:57
  • Without including your test code, this question is low quality and unlikely to help anyone else. – Adrian Larson Aug 14 '18 at 14:11
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.

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.