You need to sign in to do that
Don't have an account?
Salesforce Developer 60
Test Class for Webservice
Hello ,
I create a webservice for consuming rest api from .net . I am facing issue in test class for this . It's gives error callout not allowed from test method. How to write test class for below controller.
I create a webservice for consuming rest api from .net . I am facing issue in test class for this . It's gives error callout not allowed from test method. How to write test class for below controller.
public with sharing class WebserviceCall { public WebserviceCall(ApexPages.StandardController controller) { } public String message {get; set;} public PageReference getaccesstoken() { string jsonstring ='userName=test@gmail.com&Password=123&grant_type=password'; HttpRequest req = new HttpRequest(); HttpResponse res = new HttpResponse(); Http http = new Http(); // req.setClientCertificateName('Client Certificate'); req.setEndpoint('http://google.com'); req.setMethod('POST'); // req.setHeader('SOAPAction', 'Soap Action'); //req.setHeader('Authorization','Auth Token'); req.setBody(jsonstring); try { res = http.send(req); message = res.getBody(); TokenInformation token = new TokenInformation(); token =(TokenInformation)System.JSON.deserialize(message, TokenInformation.class); // JSONParser parser = JSON.createParser(res.getBody()); System.debug(token.access_token); //HttpServletResponse httpResponse = (HttpServletResponse)response; string s = token.access_token; string a1 = s.substring(0,255); string a2 = s.substring(255,510); string a3 =s.substring (510,s.length()); //Here SessionData__c is custom setting used for store access_token SessionData__c objToken = new SessionData__c(); objToken.Name ='token2'; objToken.Id ='a0G28000000ENAI'; objToken.Access_Token1__c =a1;objToken.Access_Token2__c =a2; objToken.Access_Token3__c =a3; upsert objToken; } catch(System.CalloutException e) { System.debug('Callout error: '+ e); message = res.toString() + res.getBody(); System.debug(res.toString()); } return null; } }
/* MockResponse class implementing the HttpCalloutMock interface */
-----------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------------
/* Test Class getting fake response from the class MockHttpResponseGenerator that implements HttpCalloutMock */
Please mark as best answer so it will help to other who will serve same problem.
Thanks!
All Answers
To test the Callout you need to create an Mock class please refer following links :
- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_wsdl2apex_testing.htm
Hope its help!!!
Thanks!
For a WebService CallOutclass Please follow the link:-http://http://learningthecloudway.blogspot.in/2013/10/how-to-create-mock-http-response-in.html
Try this and mark as best if it helps for you
/* MockResponse class implementing the HttpCalloutMock interface */
-----------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------------
/* Test Class getting fake response from the class MockHttpResponseGenerator that implements HttpCalloutMock */
Please mark as best answer so it will help to other who will serve same problem.
Thanks!
Please mark as best answer so it will help to other who will serve same problem.
Thanks!
catch(System.CalloutException e){
message = res.toString() + res.getbody();
}
is Covered that mean, the response have some issue, to solve it we need that message.
Please check for line System.debug('Callout error : ' + e)
So now catch(System.CalloutException e) and message = res.getBody(); not coverage.
please mark as best answer so it will help to other who will serve same problem.
Thanks!
public class AlohaSfaRestAPI {
public static void getOppInfo(){
HttpRequest req = new HttpRequest();
req.setEndpoint('https://mydev.my.salesforce.com/services/oauth2/token?grant_type=password&client_id=3MVG9ZPHiJTk7yFwWsQb9T5TXh2bqxM2K3gZpL2yHTVW06OzEVEqL5gF.zjZx9MbHDP1ytYiNGtKjd6Z2PHEP&client_secret=AF_AB_894B96704F6816EA82C0D8BE96A37C309ABBD3213B250D4ABAFDDE57EA05&username=myusername&password=$mypassword');
req.setMethod('POST');
Http http = new Http();
HttpResponse res = new HttpResponse();
res = http.send(req);
//System.debug('response autho --- ' +res.getBody());
Oauth objAuthInfo = (Oauth)JSON.deserialize(res.getBody(), Oauth.class);
if(objAuthInfo.access_token != null) {
HttpRequest reqnext = new HttpRequest();
reqnext.setEndpoint('https://mydev.my.salesforce.com/services/apexrest/getOppInfo/Opportunity');
reqnext.setMethod('GET');
reqnext.setHeader('Content-Type', 'application/json');
reqnext.setHeader('Authorization', 'Bearer '+objAuthInfo.access_token);
Http httpnext = new Http();
HttpResponse resnext = httpnext.send(reqnext);
//System.debug('Opportunity Info --- ' +resnext.getBody());
}
}
public class Oauth {
public string access_token{get; set;}
public string instance_url{get; set;}
public string id{get; set;}
public string token_type{get; set;}
public string signature{get; set;}
}
}