Reputation: 7639
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?
Upvotes: 4
Views: 8873
Reputation: 11307
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
global class Feedpost9 {
// your methods here...
static testMethod void testRest() {
// set up the request object
System.RestContext.request = new RestRequest();
RestContext.request.requestURI = '/feedpost/whatever';
RestContext.request.addHeader('email', '[email protected]');
// Invoke the method directly
Feedpost9.updateCase();
}
}
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...
Upvotes: 5