You need to sign in to do that
Don't have an account?
How to unit test v24 Apex REST classes?
Perhaps I'm missing something here but I can't find any docs or way to write unit tests for Apex REST classes for v24. Here's my class:
@RestResource(urlMapping='/v.9/member/*/results/*') global with sharing class MemberRestSvc { @HttpGet global static String doGet() { String[] uriKeys = RestContext.request.requestURI.split('/'); // do awesome programming stuff here } }
With a v23, I would typically write a test like:
RestRequest req = new RestRequest(); RestResponse res = new RestResponse(); req.requestURI = 'https://cs9.salesforce.com/services/apexrest/v.9/member/me/results/today'; req.httpMethod = 'GET'; String results = MemberRestSvc.doGet(req, res); System.assertEquals('awesomeness-happened',results);
However, I can't figure out how to specify the requestURI. It looks like the the System.RestContext class is not writeable? I keep getting NPEs. Any help would be greatly appreciated.
Thanks
Jeff Douglas
Appirio / CloudSpokes
Here is the code that is working for me. All you have to do is assign the RestRequest req to RestContext.request.
All Answers
Here is the code that is working for me. All you have to do is assign the RestRequest req to RestContext.request.
Not sure if this is a workaround, or the official way to do this, but you could code something like
Your test code would then be something like:
Cheers,
Pat
Looks like Kartik was working on this at the same time as me. If his version works for you, Jeff, it looks better than mine, but from your message, it sounded like you'd tried this approach unsuccessfully?
My initial test seems to indicate that Kartik's solution works. Still verifying.
My solution is also same as kartik's solution. Thats the way to test v24 Apex Rest class.
Do You guys have any idea whether now atleast can we utilize Winter 13 release mock callout methods for this ?
Hi Kartik,
Thank you for sharing your test code for a Get method. This is very helpful! Could you also post an example of a simple unit test for a Post method that accepts parameters in the request body?
Thanks in advance for your help.
Hello mandyc,
Set the req.addParameter and set the req.httpMethod to "POST', Below is the sample -
Thank you for your quick response! I'm getting the following error: Variable does not exist: JSONMsg.
You can mock the POST response message in JSONMsg
Something like
String JSONMsg = '{"message":"success"}';
I'm not having any luck. Here's my current error: Method does not exist or incorrect signature.
Here's my test code:
I'm wondering if I need to be using requestBody rather than addparameter given the below code?
Please try the above .I hope this is clear and also use Test.Start Test and Test.Stop Test Methods for test classes as best practice.Thanks
Thank you, Mohith! Your example worked well for my doPost method.
Now I'm using your example to attend a test method for a doPatch call but having issues with the test code not firing. Here is my code if you have any suggestions.
Please paste the class for PATCH call.I will have a look .Thanks.Please Mark as answer for POST call as that may help others .
Here is the PATCH call:
Below is the latest test code for the PATCH call. Note, I'm getting the following error: List has no rows for assignment to SObject on the following line about half way through the below code: String orgId = [SELECT Id from Account LIMIT 1].Id;
I have @isTest listed at the top of my class.
I tried to mark your earlier response as the answer but I wasn't given the option. Maybe this is due to a different post being marked as the answer by another user?
Thank you for your help!!!
Insert the account in your test class .And then the exception will disappear
Thank you, Mohit! That solved the problem.
Most of my PATCH code is not being tested. I think it's because I'm creating new lead and contact records in the tests but then the following lines do not return anything for the new test sample_ids I'm sending.
Here is a screen shot of the patch code (click "Test Code" hyperlink) that is not being fired by my test code (included below). Does anyone have thoughts around why line 68 is returning false?
Test Code
Here is my test code:
I need the delete method for the Apex rest class : Please provide sample code