You need to sign in to do that
Don't have an account?
adityaMorgan
Urgent requirement Rest Service Test Class
My class is similar to this class:
@RestResource(urlMapping='/MyRestContextExample/*')
global with sharing class MyRestContextExample {
@HttpGet
global static Account doGet() {
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
Account result = [SELECT Id, Name, Phone, Website FROM Account WHERE Id = :accountId];
return result;
}
}
Test method :
public static testMethod void AttendeeTest(){
createData();
String contactId = [Select Id from Contact where Email= : 'testNew@test.com'].Id;
test.startTest();
RestRequest req = new RestRequest();
RestResponse res = new RestResponse();
req.requestURI = 'https://ap1.salesforce.com/MyRestContextExample/'+contactId;
req.httpMethod = 'GET';
system.debug('---requestURI'+req.requestURI); // here i am getting correct URL
List<Account> atndInsert = MyRestContextExample.doGet();
test.stopTest();
}
Above method is returning null pointer exception at line
String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
On debugging I found RestRequest req has value null so req.requestURI is throwing exception. But I am providing request uri in my test class.(req.requestURI= 'https://ap1.salesforce.com/MyRestContextExample/'+contactId;). Then how come it is null.
Please help.
@RestResource(urlMapping='/MyRestContextExample/*')
global with sharing class MyRestContextExample {
@HttpGet
global static Account doGet() {
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
Account result = [SELECT Id, Name, Phone, Website FROM Account WHERE Id = :accountId];
return result;
}
}
Test method :
public static testMethod void AttendeeTest(){
createData();
String contactId = [Select Id from Contact where Email= : 'testNew@test.com'].Id;
test.startTest();
RestRequest req = new RestRequest();
RestResponse res = new RestResponse();
req.requestURI = 'https://ap1.salesforce.com/MyRestContextExample/'+contactId;
req.httpMethod = 'GET';
system.debug('---requestURI'+req.requestURI); // here i am getting correct URL
List<Account> atndInsert = MyRestContextExample.doGet();
test.stopTest();
}
Above method is returning null pointer exception at line
String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
On debugging I found RestRequest req has value null so req.requestURI is throwing exception. But I am providing request uri in my test class.(req.requestURI= 'https://ap1.salesforce.com/MyRestContextExample/'+contactId;). Then how come it is null.
Please help.
to set the request for use before calling the request. This will enable your service to use those values.
All Answers
to set the request for use before calling the request. This will enable your service to use those values.
Thnaks, for the help. :)
So when a call is made to a REST web service the request is stored in the RestContext object as the RestContext.request and is of type RestRequest. It is used for retrieving data from the request including the body parameters and other information. You then set the RestResponse instance in the RestContext.response instance variable with the correct status and other infromation (including information in the response body) to be returned from the apex rest method.
Could you please explain it further "So when a call is made to a REST web service the request is stored in the RestContext object". Here what exactly is request. Is it requestURI, httpMethod etc.?
So in my test class by adding RestContext.request = req, I am storing RestRequest parameters in RestContext. Am I correct? And I think by using RestRequest i can check the response in system.Assert ?
That then allows you to access the information about the request. You should then assert on your Account being returned properly. Responses are used with more complex data setups.