function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Admin DNAAdmin DNA 

test class sending sms

Hi,
I can't test this class...
public class smscontroller {
    //Send SMS (Routee)
    @AuraEnabled
    public static void sendsms(String WhoId, String WhatId, String testo, String destinatario){    
        //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 request = new HttpRequest();
        request.setEndpoint('https://auth.routee.net/oauth/token');
        request.setMethod('POST');
        request.setHeader('authorization', 'Basic xxxxxxxxxxxxxxxxx');
        request.setHeader('content-type', 'application/x-www-form-urlencoded');
        request.setBody('grant_type=client_credentials');
		HttpResponse response = http.send(request);
        // 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 request1 = 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();
                            request1.setEndpoint('https://connect.routee.net/sms');
                            request1.setMethod('POST');
                            request1.setHeader('authorization', 'Bearer '+ access_token);
                            request1.setHeader('content-type', 'application/json');
                            request1.setBody(bodyS);
                            HttpResponse response1 = http.send(request1);
                        	String json1 = response1.getBody();

                            //Insert Log Task
                            Task Task = new Task();
                            task.WhoId = Whoid;
                            task.WhatId = WhatId;
                            task.ActivityDate = system.today();
                            task.Subject = json1 ;
                            task.Description = json1;
                            insert task; 
                        }
                    }
        		}
    	}
}

Any ideas?
Thankyou
Santosh Reddy MaddhuriSantosh Reddy Maddhuri
For any REST callouts test class to test you need to have 2 things.

1. Wrtie mock https class to test the above secnario
2. Call the above class method in regular test class

Synatax snippet
@isTest global class MockTestClass implements HttpCalloutMock {
 // Implement this interface method
 global HTTPResponse respond(HTTPRequest request) { 
//Create a fake exception. CalloutException e = (CalloutException)CalloutException.class.newInstance(); 
e.setMessage('Create an exception if testing an exception scenario.');
throw e; 
// Create a fake response 
String mockJSONstring = '{ }'; 
HttpResponse response = new HttpResponse();
 response.setHeader('Content-Type', 'application/json'); response.setBody(mockJSONstring); 
response.setStatusCode(200); return response;
} 
} 

Then call the above mock class in the actual test class with following syntax 

Test.StartTest();

 Test.setMock(HttpCalloutMock.class, new MockTestClass()); 

// Other test methods if required.

 Test Stoptest();


Hope this helps!