I have stored the SharedSecret and AccesskeyId in a custom setting.

The shared secret has reserved characters. This is a sample sharedsecret 'abc'\1@pQr&^%HGF45abcc'

In apex code I am accessing the shared secret like this

CustomSetting__c cs = CustomSetting__c.getInstance();
string secretAccessKeyUnEsc =cs.SecretAccessKey__c;
string secretAccessKey  = secretAccessKeyUnEsc.unescapeJava();

However my test class fails.

public class MyController {

    public static String getAPIData(String dealerId){

        // Asign the custom setting values
        CustomSetting__c cs = CustomSetting __c.getInstance();

       string algorithmName = cs.Algorithmn__c;  

       string accessKeyID = cs.AccessKeyId__c;
       system.debug('AccessKeyId: ' +accessKeyID);  

       // escape the reserved characters (‘ and / from sharedsecret)
       string secretAccessKeyUnEsc =cs.SecretAccessKey__c;
       string secretAccessKey  = secretAccessKeyUnEsc.unescapeJava();
       system.debug('SharedSecret: ' +secretAccessKey);  

       string host = cs.Host__c;
       system.debug('Host: ' +host); 
       string endpoint = cs.End_Point_URL__c +dealerId;
       system.debug('Endpoint: ' + endpoint);   
    public static List<Map<string, integer>> getGraph(String accountid){

Here's my test class

      private class MyControllerTest {
        static testMethod void validateChart() {
       Id RecordTypeIdAcc = 
       Schema.SObjectType.Account.getRecordTypeInfosByName().get('Sales Account').getRecordTypeId();
       Account obj = New Account(Name='Test',External_ID__c='KZ4WE',recordtypeid = RecordTypeIdAcc);
       insert obj;

       // Custom setting record 
       CASIntegration__c mycs = CASIntegration__c.getInstance();

       CASIntegration__c csobj = New CASIntegration__c();
       csobj.AccessKeyId__c= mycs.AccessKeyId__c;
       csobj.SecretAccessKey__c = mycs.SecretAccessKey__c.unescapeJava();    

       csobj.Algorithmn__c = mycs.Algorithmn__c;
       csobj.Host__c = mycs.Host__c;
       csobj.End_Point_URL__c =  mycs.End_Point_URL__c;
       insert csobj;

       Test.setMock(HttpCalloutMock.class, new MyAuthMock());  

The error:

System.NullPointerException: Attempt to de-reference a null object

At this line - 
string secretAccessKey  = secretAccessKeyUnEsc.unescapeJava();

1 Answer 1


You haven't even shared if your Custom Setting is List or Hierarchy type. I will assume the latter, but basically the problem is that CustomSetting__c.getInstance().SecretAccessKey__c is null. You have to create your own test data, which you seem to understand based on the fact you insert an Account to test against. The same goes for custom settings. They are just data. You need to insert a CustomSetting__c record which will be retrieved when you call getInstance, and on that record you need to populate SecretAccessKey__c.

  • Thanks @adrianLarson for the response. As suggested, I have created the custom setting data in test class, yet its failing. Please See edited body of the question Aug 23, 2019 at 17:53

You must log in to answer this question.

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