I am developing a Rest Web service in salesforce.com platform. I wrote it and it is working properly and giving accurate responses when I call it. The problem is how to test it
@RestResource(urlMapping='/feedpost/*')
global class Feedpost9
{
@HttpPut
global static User updateCase() {
RestRequest req=RestContext.request;
User user=[SELECT id from User where email=:req.headers.get('email') LIMIT 1];
return user;
}
}
Now my task is to test this REST web service method updateCase. For testing a POST request method I have to set the parameters in method call but in case of PUT method – how do I set RestContext.request in the test class?
There are two issues you must contend with in your unit tests:
Here is basic technique to setting up the request object your method wants
The other problem you’re likely to have is that in Salesforce v24 and above unit-tests, by default, do not have access to your data. That’s because unit tests run in a kind of sandbox that have none of your data, but do contain your metadata (RecordTypes, etc). So, there are two techniques for getting data to your tests:
@isTest(SeeAllData=true)annotation on your method. This will give the test method access to your data.I prefer to use the later method, but sometimes I cheat…