0

I have 2 RestResource classes:

@RestResource(urlMapping='/Catcher/*')
global Without Sharing class vecCatcher {

    @HttpGet
    global static void httpGet() {
       ...
     }
}

and

@RestResource(urlMapping='/MemberCallTwilio/*')
global without sharing class MemberCallTwilio {
    @HttpGet
    global static void httpGet() {
     ...
    }
}

I'm writing the unit test for the second one, in which I set the requestURI to '/MemberCallTwilio/p2p'. When I run the test, it calls the Catcher class instead. I've even got a debug statement in Catcher that shows the requestURI is 'MemberCallTwilio/p2p'.

When we test this real world, the incoming rest call accesses the correct class, it's just the unit test that results in a call to the wrong class. Catcher runs, system debugs all the parameters correctly, then does nothing because the statusCallbackEvent has no actions in this class.

Here's the full test, along with the method that generates the HTTP request:

 @isTest
    public static void test_EnglishCallsSpanish(){
        List<Contact> members = [
            SELECT Id, FCM_Token__c, FirstName, LastName 
            FROM Contact 
            WHERE FirstName = 'MemberApp' 
            ORDER BY LastName 
            LIMIT 3];
        Contact caller = members[2]; //English, has fcmToken
        Contact recipient = members[1]; //Spanish, has fcmToken
        RestRequest req = generateCallRequest(caller.id, recipient.id);
        RestContext.request = req;
        RestResponse res = new RestResponse();
        RestContext.response = res;
        //set mock for pushNotification callout
        Test.setMock(HttpCalloutMock.class, new HttpMockGenerator());
        test.startTest();
        vecCatcher.httpGet();
        test.stopTest();
        System.assertEquals(200, res.statusCode);
        //more asserts to come
    }

    private static RestRequest generateCallRequest(String callerId, String recipientId){
        RestRequest req = new RestRequest();
        req.addHeader('Content-Type', 'application/json');
        req.requestURI = '/MemberCallTwilio/p2p';
        req.httpMethod = 'GET';
        req.addParameter('RoomStatus', 'in-progress');
        req.addParameter('RoomType', 'peer-to-peer');
        req.addParameter('recipientAccessToken', 'eyJhbGciOiJIUzI1Ni');
        req.addParameter('RoomSid', 'RM712345');
        req.addParameter('RoomName', '003jsotNO8');
        req.addParameter('SequenceNumber', '0');
        req.addParameter('recipientId', recipientId);
        req.addParameter('callerId', callerId);
        req.addParameter('StatusCallbackEvent', 'room-created');
        req.addParameter('Timestamp', '2021-12-13T16:08:05.694Z');
        req.addParameter('AccountSid', 'AC...2345');
        System.debug('🔸 requestURI: ' + req.requestURI);
        return req;
    }
1
  • BTW, I know I don't need test mock for testing an incoming REST call, but the MemberCallTwilio class results in an outbound call that does need the mock. Dec 15, 2021 at 18:32

1 Answer 1

2

Your Catcher endpoint is receiving the request because in your test you have veeCatcher.httpGet();

Change that to MemberCallTwilio.httpGet(), and you'll execute the code you're intending to execute.

2
  • DUH! Copy/paste error. Thanks for fresh eyes! Dec 15, 2021 at 18:38
  • @PatMcClellan__c it's always the small things, isn't it?
    – Derek F
    Dec 15, 2021 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.