+ Start a Discussion
ManojKumar MuthuManojKumar Muthu 

Test class for future CallOut method

Can someone help with test call for the below code,

Public with sharing class Restcall
{
@future(callout=true)
public static  void callwebservice(Id Accid, String name, String email, String title)

{
HTTP auth = new HTTP()    ;
HTTPRequest r = new HTTPRequest();
r.setHeader('Content-Type','application/json');
String username = 'name@yyy.com';
String password = 'yyy@123';
String loginresponse = 'null';
String Token = 'null';
r.setEndpoint('https://example/v1/users/login');
r.setMethod('POST');
r.setBody('{"email":"name@yyy.com","password":"yyy@123"}');
HTTPResponse authresp=new HttpResponse();
authresp = auth.send(r);

JSONParser parser = JSON.createParser(authresp.getBody());
System.debug('parser '+ parser);
//Token = parser.getText();
While (parser.nextToken() != null){
if((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'access_token') )
{
 String fieldvalue= parser.getText();
 parser.nextToken();
 token = parser.getText();
  

   System.debug('parser.getCurrentName() => '+ parser.getCurrentName());
   System.debug('parser.getText() => '+ parser.getText());
   
   System.debug('Token Name=> '+ fieldvalue);
      System.debug('Token Value  => '+ token );
   
   }
     
}
      

HTTP auth1 = new HTTP()    ;
HTTPRequest r1 = new HTTPRequest();
r1.setHeader('Content-Type','application/json');
r1.setHeader('Authorization', 'Bearer ' +Token);
r1.setEndpoint('https://example/v1/accounts/updateaccountdetails');
r1.setMethod('POST');  
system.debug('Account ID'+ accid);
system.debug('LEVEL 2 Name'+ name);
system.debug('level_2_email'+ email);
system.debug('level_2_title'+ title);
Account_Case_Escalation__c jsonb = new Account_Case_Escalation__c();
r1.setBody('{"account_id":"'+accid+'", "level_2_name":"'+name+'", "level_2_email":"'+email+'", "level_2_title":"'+title+'"}');
HTTPResponse authresp1=new HttpResponse();
authresp1 = auth1.send(r1);
system.debug('Statuscode Expected'+ authresp1.getStatusCode());
system.debug('Body expected' + authresp1.getBody());
 }
}

Thanks in advance.