You need to sign in to do that
Don't have an account?

unable to test class httprequest
Hi,
I can't test this class...
any idea?
Thanks in advance
I can't test this class...
any idea?
//Send SMS (Routee) public class sms { @AuraEnabled public static void sendSms(String destinatario, String testo){ //Get Current User MobilePhone String mittente = [Select MobilePhone From User Where Id = :UserInfo.getUserId()][0].MobilePhone; mittente = mittente.deleteWhitespace(); //Routee Authentication and Get Token Http http = new Http(); HttpRequest req = new HttpRequest(); req.setEndpoint('https://auth.routee.net/oauth/token'); req.setMethod('POST'); req.setHeader('authorization', 'Basic xxxx'); req.setHeader('content-type', 'application/x-www-form-urlencoded'); req.setBody('grant_type=client_credentials'); HttpResponse response = http.send(req); // Parse the JSON response if (response.getStatusCode() != 200) { System.debug('The status code returned was not expected: ' + response.getStatusCode() + ' ' + response.getStatus()); } else { System.debug(response.getBody()); String json = response.getBody(); JSONParser parser = System.JSON.createParser(json); while (parser.nextToken() != null) { if(parser.getCurrentName() == 'access_token') { parser.nextValue(); System.debug(parser.getText()); String access_token = parser.getText(); // Send SMS Http http1 = new Http(); HttpRequest req1 = new HttpRequest(); JSONGenerator body = System.JSON.createGenerator(true); body.writeStartObject(); body.writeStringField('from', mittente); body.writeStringField('to', destinatario); body.writeStringField('body',testo); body.writeEndObject(); String bodyS = body.getAsString(); req1.setEndpoint('https://connect.routee.net/sms'); req1.setMethod('POST'); req1.setHeader('authorization', 'Bearer '+ access_token); req1.setHeader('content-type', 'application/json'); req1.setBody(bodyS); HttpResponse response1 = http.send(req1); } } } } }
Thanks in advance
We can test HTTP Callouts by Implementing the HttpCalloutMock Interface.
More details :
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm
https://developer.salesforce.com/blogs/developer-relations/2013/03/testing-apex-callouts-using-httpcalloutmock.html
Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.
Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
I have already tried this way, but I can't save my test class because of the error "Method does not exist or incorrect signature: void sendSms(String, String) from the type smsTest".
My test class: