Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
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());

}

}

 
4 answers
  1. Apr 5, 2016, 8:55 PM
    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());

        

        

    }

    }
  2. Apr 5, 2016, 9:58 PM
    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());

    }

    }

     

     
  3. Apr 5, 2016, 8:54 PM
    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 

     
0/9000