0

I am trying to add custom settings data into my Test DataFactory so as to increase test coverage.

I currently create data for custom settings in my test datafactory.This is both time consuming and tedious to maintain.

Here is how a simplified version of the datafactory

@isTest(seeAllData=false)
public class Test_DataFactory {

public static void createMyAppCustomSettings() {
        List<MyAppCustomSettings__c> applst = new List<MyAppCustomSettings__c>();
        applst.add(new MyAppCustomSettings__c(Name = 'Test 1'));
        applst.add(new MyAppCustomSettings__c(Name = 'Test 2'));
        Database.insert(applst,true);
  }

}

Then i call the createMyAppCustomSettings() method in testSetup in a test class.How can i add make MyAppCustomSettings__c without me having to create the data manually.

@isTest(seeAllData=false)
private class Test_MyAppCustomSettings {
    @testSetup
    static void buildTestClassData() {
       Test_DataFactory.createMyAppCustomSettings();
    }

}
3

You're meant to insert Custom Settings during a unit test (at least, with isolation enabled). You should choose to insert custom settings when testing code that depends on those custom settings. If you really want to use existing settings, you can use SeeAllData=true, but this is not recommended. Do not do it. Insert your Custom Settings as you're meant to do.

  • Is there a way i can make only the data for the custom settings only visible.I already tried adding SeeAllData=true in the testdatafactory but i still wasn't getting the records – user2650277 Jun 10 '18 at 8:01
  • @user2650277 You need to use SeeAllData=true on the class where the test runs. A utility class can't specify their own isolation. – sfdcfox Jun 10 '18 at 8:03
  • Ohh but if i do that for the Test_MyAppCustomSettings , then all data for the org will be visible.Is there a way to write the Test_MyAppCustomSettings in such a way that only MyAppCustomSettings__c is visible – user2650277 Jun 10 '18 at 8:09
  • @user2650277 Nope. It's all or nothing. The official recommendation is to insert any custom settings that you need. – sfdcfox Jun 10 '18 at 8:11
  • developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… seems to be a better option.What do you think – user2650277 Jun 10 '18 at 8:16
0

You can use custom metadata types instead of custom setting, it behaves same as custom settings and it is also available in test classes without using seealldata=true annonation

here is the salesforce trailhead link for the it describing same https://trailhead.salesforce.com/en/modules/custom_metadata_types/units/custom_metadata_types_create#

  • That's partially true. Custom Metadata Types are often less suitable for application configuration (as opposed to support data) because they cannot be manipulated in Apex as Settings can - there's currently no support for deleting them from Apex, and Create/Update workflows are much clunkier. – David Reed Jun 10 '18 at 12:26

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.