[SalesForce] (Solved) Writing Apex Test for Twilio SMS and Twilio Call

I have working Code the sends an SMS or Voice call via Apex and Twilio, Great stuff. The trouble I am having is writing the Tests (maybe because its Friday?) and I just need a little help. I don't do near enough Apex for my liking and I am feeling it here.

I know I should be writing a Http mock someplace in my test…Any help/pointers would be most appreciated.

Working SMS:

public class TwilioSMSInvocable {
    @InvocableMethod
    public static void callPhoneService(List<ID> caseIDs) {
        List<Twilio_SMS__c> SelectTwilio_SMS = [Select Id, MobileNumber__C, Message__c from Twilio_SMS__c Where ID in :caseIDs] ;
    for (Twilio_SMS__c t : SelectTwilio_SMS){
           Map<String,String> params = new Map<String,String> {
             'To'   => t.MobileNumber__c,
             'From' => '13176666666',                
             'Body' => t.Message__c
         };

            DoCallout(params);      
     }
        }

    @future(callout=true)

    private static void DoCallout(Map<String, String> params){
        String account = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
        String token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
        TwilioRestClient client = new TwilioRestClient(account, token);
        TwilioSMS sms = client.getAccount().getSMSMessages().create(params);
         System.Debug('TwilioSMS ' + sms) ;
    }
}

Working Voice:

public class TwilioCallInvocable {

    @InvocableMethod
    public static void callPhoneService(List<ID> caseIDs) {
        List<Twilio_Voice__c> SelectTwilio_Voice = [Select Id, Say__c, URL_Build__c from Twilio_Voice__c Where ID in :caseIDs] ;
    for (Twilio_Voice__c t : SelectTwilio_Voice){
           Map<String,String> params = new Map<String,String> {
        'To'   => t.MobileNumber__c,
        'From' => '+13176666666',               
            'Url' => t.URL_Build__c
        };

            DoCallout(params);      
       }
    }

    @future(callout=true)

    private static void DoCallout(Map<String, String> params){
        String account = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
        String token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
        TwilioRestClient client = new TwilioRestClient(account, token);
        TwilioCall call = client.getAccount().getCalls().create(params);
            system.debug('TwilioCAL' + call);
    }
}

Test Class:

@isTest
private class TwilioSMSTest 
{
    static testmethod void testTwilioSMSInvocable() 
    {
        Map<String,String> params = new Map<String,String> {
             'To'   => '+17602444355',
             'From' => '13176664444',               
             'Body' => 'test message'
         };
            String account = 'AC6a217d8adb3460b5870c05bc6c666666';
        String token = '397c3f614cd3cf783154dc2383234444';
        TwilioRestClient client = new TwilioRestClient(account, token);
        TwilioSMS sms = client.getAccount().getSMSMessages().create(params);
        test.startTest();
             Test.setMock(HttpCalloutMock.class, new TwilioSMSTest());
        test.stopTest();
    }
}

Error Message "TwilioRestException: The requested resource was not found more info: null" , and 0 test coverage

Updated Test Class

 Twilio_SMS__c txt = new Twilio_SMS__c();
    txt.Name = 'Tom';
    insert txt;

  // List<Twilio_SMS__c> insertedRecord  = [Select Id, Name from Twilio_SMS__c Where Name ='Tom'];
    // used--> TwilioSMSInvocable.callPhoneService(insertedRecord); 
    // -- error -- Method does not exist or incorrect signature: void callPhoneService(List<Twilio_SMS__c>) from the type TwilioSMSInvocable
    // 
  //  Twilio_SMS__c insertedRecord  = [Select Id, Name from Twilio_SMS__c Where Name ='Tom'];
    // used--> TwilioSMSInvocable.callPhoneService(insertedRecord);
    // -- error --- Method does not exist or incorrect signature: void callPhoneService(Twilio_SMS__c) from the type TwilioSMSInvocable

         Test.setMock(HttpCalloutMock.class, new TwilioSMSTest());
   test.startTest();
    // see documented attempts above
          TwilioSMSInvocable.callPhoneService(insertedRecord); 
                    // txt above -- error-- Method does not exist or incorrect signature: void callPhoneService(Twilio_SMS__c) from the type TwilioSMSInvocable
    test.stopTest();

What am I doing wrong?

Best Answer

Rewriting just the section between startTest and stopTest:

// Create your case(s)
// Create Twilio_SMS__c records
// Query cases to new List caseList

Test.setMock(HttpCalloutMock.class, new TwilioSMSTest());
test.startTest();
TwilioSMSInvocable.callPhoneService(caseList);
test.stopTest();

Do the same for the other class, in a separate test method. This could be in the same test class.

Related Topic