0

I'm having trouble testing this code. It seems so simple but I can't get it to work.It keep giving me an Attempt to de-reference a null object error.

public CustomSetting__c instanceVariable;
public CustomSetting__c getCustomSettingInstance(){
     return instanceVariable;
}

EDIT: Forgot to add the test methods but here they are

@isTest static void test_getConfigInstance() {
    CustomSettingCtrlr myCtrlr = new CustomSettingCtrlr ();

    myCtrlr.instanceVariable.Name = 'name';
    myCtrlr.instanceVariable.booleanField= true;
    insert myCtrlr.instanceVariable;

    Test.startTest();
        myCtrlr.getConfigInstance();
    Test.stopTest();

    System.assertNotEquals(csmUtil.getConfigInstance(), null);
    System.assertEquals(myCtrlr.instanceVariable.Name, 'name');

}

closed as off-topic by Eric, Santanu Boral, Markus Slabina, glls, battery.cord Aug 23 '17 at 17:31

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on problems in code you've written must describe the specific problem and include valid code to reproduce it. For help writing short, self-contained syntactically-valid examples, see: SSCCE.org" – Eric, Santanu Boral, Markus Slabina, glls, battery.cord
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Possible duplicate: salesforce.stackexchange.com/questions/174857 – sfdcfox Aug 23 '17 at 14:03
  • @AdrianLarson Duplicate? – sfdcfox Aug 23 '17 at 14:03
  • There's a little bit more going on in that post, but this scenario is one of the aspects of that post. Probably not worth reopening just to mark as duplicate though. – Adrian Larson Aug 23 '17 at 14:05
  • 1
    Please be more specific than "I can't get it to work". – Adrian Larson Aug 23 '17 at 14:19
  • 2
    This will not compile as the instanceVariable is private and thus not accessible to the test method (unless you are using an OLD API and the test is in the same class). Please post the code as it is compiled AND the EXACT error message verbatim – Eric Aug 23 '17 at 16:13
3

Ok, so aside from your code not compiling:

  1. Your declaration of instanceVariable starts out as null

    public CustomSetting__c instanceVariable;
    
  2. then in your test when you do the following

    myCtrlr.instanceVariable.Name = 'name';
    

You will get a DNO error as the variable is null

To Fix, add the following before that line

myCtrlr.instanceVariable = New CustomSetting__c();

Depending on what you are actually trying to do you may want to automate it and change the get method to the following:

public CustomSetting__c getCustomSettingInstance(){
     instanceVariable = CustomSetting__c.getOrgDefaults();
     //If the Id or variable is null then instantiate a new instance
     return instanceVariable;
}

Additionally your asserts will cause confusion as the pattern is

EXPECTED,ACTUAL

System.assertEquals('name', myCtrlr.instanceVariable.Name);

The way you have it reverses that order

0

I'm not sure if i got it correctly i'm also a beginner but, try adding @TestVisible annotation in front of 'private CustomSetting__c instanceVariable;' in your controller.

e.g @TestVisible private CustomSetting__c instanceVariable;

then simply call the method in test class Like: CustomSettingCtrlr myCtrlr = new CustomSettingCtrlr (); myCtrlr.getCustomSettingInstance();

Hope this will help.

  • So the private variable is public in my code, I accidentally wrote it in the code – Kush Patel Aug 23 '17 at 16:29
  • static testMethod void MyTestMethod(){ CustomSettingCtrlr CusCtrl = new CustomSettingCtrlr(); CusCtrl.instanceVariable = new CustomSetting__c(); CusCtrl.getCustomSettingInstance(); } Try this and add field if necessary! – Ajaysingh Aug 24 '17 at 13:54

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