+ Start a Discussion
Internal PartnerInternal Partner 

Use of custom setting data in a test class

Hi dear developers,

I am pretty confused about the use of custom settings in APEX test classes.

One of our developers, who unfortunately is not there anymore, wrote the following test data in a test method:
 
static testMethod void testMethodExample() {
        insert new CS_Example__c(Batch_Limit__c = 1, EmailAddress__c='test@example.com', Name = 'Test_CS');
the test class works, but we don't understand why he created in addition to the test data within the test class, a new Custom Setting record with the name 'Test_CS". That means, he created two records on the Custom Setting CS_Example__c:

1. Name: CS_Config
    Batch_Limit__c = 200,
   Email_Address__c = test2@example.com
 
2.  Name: Test_CS
    Batch_Limit__c = 200,
   Email_Address__c = test@example.com

And he had been using the name of the second Custom Setting record only for the test class. He had been using the first Custom Setting record for the APEX (non-test) batch job class.

Or, the second record is not needed and can be deleted?.


 
Raj VakatiRaj Vakati
 Test class data wnt be committed to databased and rollback atomaucatically after test class is executed .. so you no need to delete the Test_CS custom settings .. 


1. If you are test class is not contains see all data to true .. you cannt able to get any value from the custom settings so we need to insert data into the custom settings in test class
 
insert new CS_Example__c(Batch_Limit__c = 1, EmailAddress__c='test@example.com', Name = 'Test_CS');

2. The above insert wll be rollbacked automatically 
 
Internal PartnerInternal Partner
Thanks Raj for responding.
So, you mean the second physical record with name "Test_CS" can be deleted. This physical record is not necessary, right?.

The developer didn't use "see all data to true", so he inserted the test data the way I mentioned.
Raj VakatiRaj Vakati
Yes  ..second physical record with name "Test_CS" will be deleted automatically 
Internal PartnerInternal Partner
Will be deleted automatically?. My question was, can be the second CS record be deleted? so, we don't need the second physical CS record...
I guess you haven't understood my question :(.

 
Raj VakatiRaj Vakati
Test_CS will be rollbacked automatically after test class ... no no need to click the delete record