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 callout class

Hi at all,
I'm trying to test this class 
//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);     
        					}

                    }
        }

   }
}


and this is test class:
@isTest
private class smsTest {

     
    @isTest static void testsendSms() {       
        // Set mock callout class 
        Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());

        
        
        // Call method to test.
        // This causes a fake response to be sent
        // from the class that implements HttpCalloutMock. 
        HttpResponse res = smsTest.sendSms('destinatario', 'testo');
        
        // Verify response received contains fake values
        String contentType = res.getHeader('Content-Type');
        System.assert(contentType == 'application/json');
        String actualValue = res.getBody();
        String expectedValue = '{"example":"test"}';
        System.assertEquals(actualValue, expectedValue);
        System.assertEquals(200, res.getStatusCode());
    }
}

Thanks in advance