I'm in a scratch org and created SalesRepAPI.apxc
as follows:
@RestResource(urlMapping='/get_sales_rep/*')
global with sharing class SalesRepAPI {
@HttpGet
global static Id getSalesRep() {
RestRequest request = RestContext.request;
// grab the caseId from the end of the URL
//String caseId = request.requestURI.substring(request.requestURI.lastIndexOf('/')+1);
Id returnMe = [SELECT Id FROM User LIMIT 1][0].Id;
return returnMe;
}
}
And then I also created SalesRepAPI_TEST.apxc
as follows (among other things jumping straight to unit-testing because for some reason I can't figure out how to get Workbench to work against a scratch org and I'm too lazy to go around grabbing my session ID, firing up Postman, etc.), which has 2 failing tests that I was hoping would pass:
@IsTest
private class SalesRepAPI_TEST {
@isTest static void testGet() {
// Set up a test request
RestRequest req = new RestRequest();
RestResponse res = new RestResponse();
req.requestUri = '/services/apexrest/get_sales_rep/';// + recordId;
req.httpMethod = 'GET';
RestContext.request = req;
RestContext.response= res;
Test.startTest();
// Call the method to test
Id salesRepId = SalesRepAPI.getSalesRep();
Test.stopTest();
// Verify results
Assert.isNotNull(salesRepId, 'Class ID');
Assert.areEqual(200, res?.statusCode); // ERROR: "Assertion Failed: Expected: 200, Actual: null"
Assert.isNotNull(res?.responseBody?.toString(), 'Response ID'); // ERROR: "Assertion Failed: Instance expected to be a non null value: Response ID"
}
}
What am I missing about getting something to actually populate into res.statusCode
?
Update:
I decided this would have to do for feeling confident I didn't botch getting SalesRepAPI.getSalesRep()
to take parameters, since at the moment, all branches of Sales_Rep_Suggester
(including null parameters) lead to the same User:
@RestResource(urlMapping='/get_sales_rep/*')
global with sharing class SalesRepAPI {
@TestVisible private static String unitTestHelperCountry;
@HttpGet
global static Id getSalesRep() {
RestRequest req = RestContext.request;
String paramCountry = req.params.get('country');
unitTestHelperCountry = paramCountry;
String paramFirstTimeCustomer = req.params.get('firstTimeCustomer');
Boolean paramBoolFirstTimeCustomer = ( String.isBlank(paramFirstTimeCustomer) ? FALSE : Boolean.valueOf(paramFirstTimeCustomer) );
Flow.Interview flw = new Flow.Interview.Sales_Rep_Suggester(
new Map<String, Object>{
'Country_of_Residence' => paramCountry
, 'First_Time_Customer' => paramBoolFirstTimeCustomer
}
);
flw.start();
User u = (User) flw.getVariableValue('Sales_Rep_User');
return u.Id;
}
}
@IsTest
private class SalesRepAPI_TEST {
@isTest static void testGet() {
// Set up a test request
RestRequest req = new RestRequest();
req.requestUri = '/services/apexrest/get_sales_rep/';
req.addParameter('country', 'Japan');
req.addParameter('firstTimeCustomer', String.valueOf(FALSE));
req.httpMethod = 'GET';
RestContext.request = req;
Test.startTest();
// Call the method to test
Id salesRepId = SalesRepAPI.getSalesRep();
Test.stopTest();
// Verify results
Assert.isNotNull(salesRepId, 'Class ID');
// Note: Apex unit tests can't validate the contents of RestResponse-typed objects, so don't bother checking for statusCode 200, a body that matches "salesRepID," etc.
// This hack is about the closest we can get to using a unit test to make sure that the SalesRepAPI is truly accepting HTTP parameters.
Assert.areEqual('Japan', SalesRepAPI.unitTestHelperCountry);
}
}