2

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?

3
  • 1
    If you get an error message, always include its text verbatim. There is no way your code doesn't throw an error, since TwilioSMSTest doesn't implement HttpCalloutMock.
    – Adrian Larson
    Commented Jan 19, 2018 at 19:54
  • I will work on that and get back with you Commented Jan 19, 2018 at 19:58
  • Your test code doesn't actually call any methods in TwilioCallInvocable or TwilioSMSInvocable. So you have zero coverage. You need to run the code you have in order to get the coverage. Commented Jan 22, 2018 at 1:48

2 Answers 2

0

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.

2
  • Hey @DavidSchach, added my attempts at the bottom of the post, do you know what I am doing wrong? Commented Jan 23, 2018 at 16:14
  • @CameronHouser Looks like you're calling a method incorrectly. Twilio has plenty of sample code for you to work from. Your method wants a list of records. At this point, your code is so messy that I honestly don't know what to tell you. Check the sample code and start over. It's all in the GitHub repository. Commented Jan 23, 2018 at 20:46
0

Got it working! Thanks everyone for your help! (I even give you the test class here)

Twilio Voice:

 public class TwilioCallInvocable {

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

        DoCallout(params);      
 }
 }

@future(callout=true)

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

Voice Test=

@isTest
private class TwilioCallTest 
{
@testSetup
static void testTwilioCallInvocable() 
{
   test.startTest();
    test.stopTest();
}
static testMethod void test_callService_UseCase1(){
    Twilio_Voice__c calls = new Twilio_Voice__c();
    calls.MobileNumber__c = '1xxxxxxxxx';
    calls.SAY__c = 'test message';
    calls.PLAY__c = 'https://pink-dog-6501.twil.io/assets/SampleAudio_0.4mb.mp3';
            insert calls;

TwilioCallInvocable.callPhoneService(new List<ID>());
}
}

Twilio SMS:

public class TwilioSMSInvocable {
@InvocableMethod
public static void callSMSService(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' => '+1xxxxxxxx',                
         'Body' => t.Message__c
     };

        DoCallout(params);      
 }
  }

@future(callout=true)

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

  }

SMS Test =

@isTest
private class TwilioSMSTest 
{
@testSetup
static void testTwilioSMSInvocable() 
{
   test.startTest();
    test.stopTest();
}
static testMethod void test_callSMSService_UseCase1(){
     Twilio_SMS__c txt = new Twilio_SMS__c();
    txt.MobileNumber__c = '1xxxxxx';
    txt.Message__c = 'test message';
            insert txt;

TwilioSMSInvocable.callSMSService(new List<ID>());
 }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .