Need help in writing a test class for REST API call through Oauth2 authentication between two Salesforce instances.

I have remote class (Method @HttpPOST) in provider instance which return sets of records. From Salesforce client instance, first i am sending request to get Access token and then sending again with some parameter to retrieve set of records. referred Salesforce https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_multistaticresourcecalloutmock.htm,

Here is test class i have written, it is still incomplete.


public class ExSearchPageControllerTest{    
    static testMethod void ExSearchPageControllerTestMethod() {
        MultiStaticResourceCalloutMock mock = new MultiStaticResourceCalloutMock();
        mock.setHeader('Content-Type', 'application/json');
        List<SearchWrapperClass> swcl=new List<SearchWrapperClass>();
        ExSearchPageController myclass1=new ExSearchPageController();

        // Set the mock callout mode
        Test.setMock(HttpCalloutMock.class, mock);
        myclass1.name='Test Name';

        ExSearchPageController myclass=new ExSearchPageController();
        myclass.name='Test Name ';
        myclass.street='Test Street Address ';
        myclass.city='Test City ';
        List<SelectOption> countryLs=myClass.countryList;
        myclass.ReturnAccessToken ();

` Issue is, in my original class i have called another instance twice, first to retrieve access token by passing secret key and username, password. In 2nd call i am passing values "test name" ect. But it failed to retrieve result as token is null.

  • If you wish to receive good answer prefer to share your code – Santanu Boral Nov 26 '16 at 20:36
  • re Edited my question – Pks Nov 27 '16 at 8:50

With the amount of information you have provided, this should get you started:

private class myTestClass{

    private static test method void myFirstTestMethod(){
        //...Do Some stuff...


Note the annotated class with @isTest

From here you might want to visit Apex Testing Trailhead to learn and expand on the answer to fit your unknown requirements

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.