1

I am trying to write a test method that calls a method in my class to get some configuration data. The method in my class returns a Map. From my test method, when I call the method in my class, I get an error which happens when I call APIService.getSystemSettings();

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

My code is below:

  private class APIServiceTest {

    @isTest static void APITest() {
      Map<String,String> envSettings = new Map<String,String>();
      envSettings = APIService.getSystemSettings();
    }

 }

Here is the class which contains the method I am calling:

public class APIService {

  static API_Settings__c apiSettings = API_Settings__c.getValues('apisettings')        
  static Map<String,String> envSettings = new Map<String,String>();

  public static Map<String,String> getSystemSettings() {
   envSettings.put('customerURL', apiSettings.customer_url__c);
  }

}

Most of my methods in APIService are methods annotateded with @Future(Callout=true) so that is why most of the variables are declared as static methods.

I am trying to determine why I am getting this error and what is needed to fix it.

  • is it hierarchical custom setting or list custom setting? – Pranay Jaiswal Jun 10 at 20:17
  • It is list custom setting @PranayJaiswal – user1669296 Jun 10 at 20:24
  • You have to insert the custom settings in testContext in testSetup. See David's answer. – Pranay Jaiswal Jun 10 at 20:25
5

In unit test context, Custom Settings are treated as data (not metadata), and hence are not visible due to test isolation.

Your unit test's setup, whether in your @isTest or an @testSetup method, must construct and insert any Custom Settings records you wish to have available to your test code.

You get a NullPointerException because there is no visible API_Settings__c instance called apisettings; it's null.

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.