0

I have a method

public static Map<String, Object> createNewAccessToken() {
        Map<String, Object> access_token = null;


        HttpRequest req = new HttpRequest();
        req.setMethod('POST');

        //set the request endpoint
        String authendpoint = authData.RequestAuthEndpoint__c;
        req.setEndPoint(authendpoint);

        //set the request headers
        //1. Content -type
        String authRequestContentType = authData.RequestAuthContentType__c;
        req.setHeader('Content-type', authRequestContentType);

        //2. Host
        String authRequestDomain = authData.RequestAuthDomain__c;
        req.setHeader('Host', authRequestDomain);

        //Body of the request
        String alg = 'RS256';
        String iss = authData.RequestIntegratorKey__c;
        String pkey = authData.RequestPrivateKey__c;
        String scope = authData.RequestScope__c;
        DocusignJWT jwtObject = new DocusignJWT(alg, authData.DSUserName__c, iss, pkey, scope, authRequestDomain);

        //Set the request body  
        String requestBody = authData.RequestAuthBody__c + jwtObject .issue();
        req.setBody(requestBody);

        //call the service
        Http http = new Http();
        HTTPResponse res = http.send(req);

        Map<String, Object> token = (Map<String, Object>)JSON.deserializeUntyped(res.getBody());

        if (res.getStatusCode() == 200) {

            access_token = token;             
        }                
        return access_token;
    }

I want to write a test method for this callout method. You can see from the code that, there are some data dependency inside the method. The dependent object is authData.

What I have done is

@isTest
private class DocusignTest {
    @isTest static void createNewAccessToken_Test() 
    {
        HttpMockFactory mock = new HttpMockFactory(200, 'OK', 'true', new Map<String,String>());     
        Test.setMock(HttpCalloutMock.class, mock);

        Map<String, Object> result = null;

        Test.startTest();
        result = className.createNewAccessToken();

        System.Debug('=====' + result);

        Test.stopTest();
        system.assertEquals('true', 'true'); 
    }

}

Please ignore system.assertEquals('true', 'true'); statement. Exception

Attempt to de-reference a null object

throws at result = className.createNewAccessToken(); because it is not getting the authData object.

How can I test the method?

1 Answer 1

1

You will need to set the className.authData property to an initialized object in your test. Its values don't have to be valid, just set them to fake values as it is the mock class that will be receiving them.

(FYI, having a public static method that requires some other logic to have run before you call it to set up a variable isn't a good way to structure code.)

2
  • Thank you @Keith. Yes, I have followed this way and it works! What is the good practice for this scenario? Should I pass authData to createNewAccessToken() ? Commented Apr 9, 2019 at 8:10
  • 1
    @NoorAShuvo Yes passing the object in would be better, because that makes the dependency more obvious.
    – Keith C
    Commented Apr 9, 2019 at 8:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .