You need to sign in to do that
Don't have an account?
CloudGeek
Test Class Problem (In Anonymous Window I get returned values but in test class always empty)
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/OSCList/'; request.httpMethod = 'GET'; request.params.put('region', 'USA'); request.params.put('segment', 'ALL'); request.params.put('product', 'ALL'); RestContext.request = request; List<OnlineSalesCatalogWrapper> results = new List<OnlineSalesCatalogWrapper>(); String currentRequestURL = URL.getCurrentRequestUrl().toExternalForm(); System.debug('currentRequestURL = '+currentRequestURL); results = OnlineSalesCatalogController.getOfferedProductsList(); System.debug('size of results = '+results.size()); } }
@isTest
public class OSCTestClass
{
@isTest(SeeAllData=true)
static 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/OSCList/';
request.httpMethod = 'GET';
request.params.put('region', 'USA');
request.params.put('segment', 'ALL');
request.params.put('product', 'ALL');
RestContext.request = request;
List<OnlineSalesCatalogWrapper> results = new List<OnlineSalesCatalogWrapper>();
String currentRequestURL = URL.getCurrentRequestUrl().toExternalForm();
System.debug('currentRequestURL = '+currentRequestURL);
results = OnlineSalesCatalogController.getOfferedProductsList();
System.debug('size of results = '+results.size());
}
}
All Answers
Starting with 24 version of API your test methods do not have access to the real database. You should build a test database from your test or if you still want to use the real database set the @isTest(SeeAllData=true) annotation
@isTest
public class OSCTestClass
{
@isTest(SeeAllData=true)
static 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/OSCList/';
request.httpMethod = 'GET';
request.params.put('region', 'USA');
request.params.put('segment', 'ALL');
request.params.put('product', 'ALL');
RestContext.request = request;
List<OnlineSalesCatalogWrapper> results = new List<OnlineSalesCatalogWrapper>();
String currentRequestURL = URL.getCurrentRequestUrl().toExternalForm();
System.debug('currentRequestURL = '+currentRequestURL);
results = OnlineSalesCatalogController.getOfferedProductsList();
System.debug('size of results = '+results.size());
}
}
SeeAllData=True really really was brilliant , which I couldn't even guess it might help.
This had me get 81% test coverage.
Please check the below final test class I have, it looks lengthy, I just need to provide difefrent URL parameter combinations here .
Can you help me optimize this to reduce lines of code ?
I am glad it worked for you.
As long as you are using multiple methods for testing different functionality of your class is good. Please make sure you are using Test.StartTest() and Test.StopTest() in test methods.
Here you can get more insight about Unit Test best practices.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm
Regards,
Naval