@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());
}
}
Updated code.@isTestpublic 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()); }} Hi,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.htmRegards,Naval Hi Sharma,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 ?
@isTest(SeeAllData=true)
public class OSCTestClass
{
static testMethod void getMeListOfProducts() {
ApexPages.StandardController ctrl;
OnlineSalesCatalogController osc = new OnlineSalesCatalogController(ctrl);
// 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());
string pwd = OnlineSalesCatalogController.getTempPassword('8');
System.debug('pwd = '+pwd);
}
static testMethod void getMethodTest2()
{
// 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', 'ALL');
request.params.put('product', 'ALL');
RestContext.request = request;
List<OnlineSalesCatalogWrapper> results = new List<OnlineSalesCatalogWrapper>();
results = OnlineSalesCatalogController.getOfferedProductsList();
System.debug('size of results = '+results.size());
}
static testMethod void getMethodTest3()
{
// 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', 'ANY');
request.params.put('product', 'Plus');
RestContext.request = request;
List<OnlineSalesCatalogWrapper> results = new List<OnlineSalesCatalogWrapper>();
results = OnlineSalesCatalogController.getOfferedProductsList();
System.debug('size of results = '+results.size());
}
static testMethod void getMethodTest4()
{
// 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', 'ALL');
request.params.put('product', 'Plus');
RestContext.request = request;
List<OnlineSalesCatalogWrapper> results = new List<OnlineSalesCatalogWrapper>();
results = OnlineSalesCatalogController.getOfferedProductsList();
System.debug('size of results = '+results.size());
}
}
Hi,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