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;
}