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?