You need to sign in to do that
Don't have an account?
CloudGeek
Apex REST Test Class Issue (When run in Anonymous window results look good)
Hi,
I have the below test class for my REST Class:
When I run this test class always the returned list is empty though actually I am able to see data on page.
And when I run this test class code from anynymous window - I get to see results are fine with no of rows as expected.
Can anyone help me understand If I am missing something here ?
I have the below test class for my REST Class:
When I run this test class always the returned list is empty though actually I am able to see data on page.
And when I run this test class code from anynymous window - I get to see results are fine with no of rows as expected.
Can anyone help me understand If I am missing something here ?
@isTest public class OSCTestClass { static testMethod void getMeListOfProducts() { // Set up a test request RestRequest request = new RestRequest(); // Set request properties String sfdcBaseURL = URL.getSalesforceBaseUrl().toExternalForm(); System.debug('sfdcBaseURL = '+sfdcBaseURL); request.requestUri = sfdcBaseURL+'/services/apexrest/onlinesalesUIcatalogueNew/'; request.httpMethod = 'GET'; request.params.put('region', 'USA'); request.params.put('segment', 'Costco'); request.params.put('product', 'ALL'); RestContext.request = request; List<OnlineSalesCatalogWrapper> results = new List<OnlineSalesCatalogWrapper>(); results = OnlineSalesCatalogController.getOfferedProductsList(); System.debug('size of results = '+results.size()); } }
The issue is with the Test Data being unavailable.
I did use (SeeAllData = True), it has got good coverage.
Can you help me optimize the below test class , (final one after updated) ? (It is lenghty as I had to write to cover different URL-Parameter combinations)
All Answers
You have to write the Mock class to test REST API calls.
Testing HTTP Callouts by Implementing the HttpCalloutMock Interface
Provide an implementation for the HttpCalloutMock interface to specify the response sent in the respond method, which the Apex runtime calls to send a response for a callout.
If this is your Class:
Mock Class:
Test Class:
Also go through the below links:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm
https://developer.salesforce.com/blogs/developer-relations/2013/03/testing-apex-callouts-using-httpcalloutmock.html
https://developer.salesforce.com/forums/?id=906F0000000BNqcIAG
Please do let me know if it helps you.
Regards,
Mahesh
I suspect you will also need to set the RestContext.response to an instance of System.RestResponse. Then check that for the response.
From Apex REST Methods (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_rest_methods.htm):
If the Apex method has a non-void return type, the resource representation is serialized into the response body.
Also, have you created the testing data that the getOfferedProductsList() method will access. I assume it is doing some type of SOQL query internally.
Thanks for quick response!
Here is my original class:
Always, the wrappers list is empty() from Test Class. But on Page data is fine.
The issue is with the Test Data being unavailable.
I did use (SeeAllData = True), it has got good coverage.
Can you help me optimize the below test class , (final one after updated) ? (It is lenghty as I had to write to cover different URL-Parameter combinations)