+ Start a Discussion
CloudGeekCloudGeek 

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 ?
 
@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());
    
    
}
}


 
Best Answer chosen by CloudGeek
Naval Sharma4Naval Sharma4
Updated code.

@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

Naval Sharma4Naval Sharma4
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 
 
Naval Sharma4Naval Sharma4
Updated code.

@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());
    
    
}
}
This was selected as the best answer
CloudGeekCloudGeek
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());
    }
}

 
 
Naval Sharma4Naval Sharma4
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.htm

Regards,
Naval