In my continuing series on custom settings semantics, here’s a test class that shows what happens when you try to access existing custom settings in tests with SeeAllData set to true and false.
The org has 2 settings:
1. A list setting with a row called ‘foo’
2. A hierarchy setting with a row for the sysadmin user and a field called color__c (the user has it set to ‘blue’)
@istest public class DataSiloCSTest {
@IsTest(SeeAllData=true) public static void
testAccessExisting() {
privatecs__c x = privatecs__c.getInstance('foo');
System.assert(x != null);
System.assertEquals('foo', x.name);
x = privatecs__c.getValues('foo');
System.assert(x != null);
System.assertEquals('foo', x.name);
x = [select name from privatecs__c where
name='foo' limit 1];
System.assert(x != null);
System.assertEquals('foo', x.name);
mycs__c a = mycs__c.getInstance(UserInfo.getUserId());
System.assert(a != null);
System.assertEquals('blue', a.color__c);
a = mycs__c.getValues(UserInfo.getUserId());
System.assert(a != null);
System.assertEquals('blue', a.color__c);
a = [select color__c from mycs__c where
setupownerid=:UserInfo.getUserId() limit 1];
System.assert(a != null);
System.assertEquals('blue', a.color__c);
}
@IsTest(SeeAllData=false) public static void
testNoAccessExisting() {
privatecs__c x = privatecs__c.getInstance('foo');
System.assert(x == null);
x = privatecs__c.getValues('foo');
System.assert(x == null);
List<privatecs__c> y = [select name from privatecs__c
where name='foo'];
System.assertEquals(0, y.size());
mycs__c a = mycs__c.getInstance(UserInfo.getUserId());
System.assert(a != null);
System.assertEquals(null, a.color__c);
a = mycs__c.getValues(UserInfo.getUserId());
System.assert(a == null);
List<mycs__c> b = [select color__c from mycs__c where
setupownerid=:UserInfo.getUserId() limit 1];
System.assertEquals(0, b.size());
}
}