You need to sign in to do that
Don't have an account?
Muhammad Hammad 8
when I run the test it gives the error: System.NullPointerException: Attempt to de-reference a null object
how to write apex test for custom settings?
How to write apex test for void method with custom settings?I have a list custom setting.
public string name {get;set;} public string name2{get;set;} public String setting_1{get; set;} public String setting_2{get; set;} public void set(){ myuser__c setting = myuser__c.getInstance('mydata'); name = setting.data__c; name2= setting.data2__c; } public void save(){ myuser__c setting = myuser__c.getInstance('mydata'); setting.data__c = setting_1; setting.data2__c = setting_1; update setting; } public void reset(){ myuser__c setting = myuser__c.getInstance('mydata'); setting.data__c = ''; setting.data2__c = ''; update setting; }
@isTest private class SettingsTest{ @testSetup static void setup() { myuser__c setting = new myuser__c(); setting.Name = 'data__c'; setting.data__c = 'test_name'; //setting.Name = 'data2__c'; //setting.data2__c = 'test_name2'; insert setting; } static testmethod void testSetCustomSettings() { Settings myclass = new Settings(); myclass.set(); } }
when I run the test it gives the error: System.NullPointerException: Attempt to de-reference a null object
Your method is expecting a record with Name "mydata". But you're creating a custom Setting with Name "data__c".
Thanks,
Ashish Singh.
All Answers
Your method is expecting a record with Name "mydata". But you're creating a custom Setting with Name "data__c".
Thanks,
Ashish Singh.