I have a controller and I wrote a test class for it, the test class is giving me the error message I mentioned in the Title. This is the controller:

public with sharing class OpenedAccountsController {

    public boolean ShowCloseLink {

      public PageReference init() {

        PageReference redirectToPage = new PageReference('');

        redirectToPage = new PageReference(EnvironmentSettings.getOpenedAccountsUrl()) ;

         return redirectToPage;

and this is the test class:

public class OpenedAccountsTest {
    static testmethod void SandboxUrl() {
        Organization org;


        PageReference pageRef = Page.OpenedAccounts;

        OpenedAccountsController controller = new OpenedAccountsController();
        String nextPage;
        if (!String.isBlank(controller.init().getUrl()))
            nextPage = controller.init().getUrl(); // The error is here


        if (org.IsSandbox==True && !String.isBlank(nextPage))
        //  System.debug('URL:'+ nextPage);



I know something is Null but I'm not sure what.

public class EnvironmentSettings {

    private static final External_Urls__c urlSetting =    External_Urls__c.getInstance();

    public static string getOpenedAccountsUrl() {
        return urlSetting.Opened_Accounts_Url__c;

  • Surely you can tell us the line number... Likely EnvironmentSettings.getOpenedAccountsUrl() is returning null? Is it based on Custom Setting data? – Adrian Larson Nov 16 '16 at 15:50
  • Are you sure the error is on the line that you marked, and not the line above (where you pass the result of getUrl() to String.isBlank()? – Derek F Nov 16 '16 at 15:51
  • Derek, you are right, it is on the line you mentioned. @AdrianLarson This is the 'EnvironmentSettings' class: 'public class EnvironmentSettings { private static final External_Urls__c urlSetting = External_Urls__c.getInstance(); public static string getOpenedAccountsUrl() { return urlSetting.Opened_Accounts_Url__c; } }' Does that help? – The Ghost Nov 16 '16 at 16:04
  • 1
    Can you edit your question to include the EnvironmentSettings class instead of putting it in a comment? Comments are limited in length and formatting, so it's rather hard to digest code placed in a comment. – Derek F Nov 16 '16 at 16:17
  • Done! I entered the EnvironmentSettings, the EnvironmentSettingsTest class did not pass either: I'm getting this error message: Class.sObjectCreator.createExternalUrls: line 396, column 1 – The Ghost Nov 16 '16 at 16:37

You have to create your own data when you run Unit Tests. In this case, you must actually insert the External_Urls__c setting.

static testMethod void testExternalUrls()
    system.assertEquals(null, External_Urls__c.getInstance().Opened_Accounts_Url__c,
        'Data should not be populated before test setup');

    External_Urls__c instance = External_Urls__c.getInstance();
    upsert instance;

    system.assertNotEquals(null, External_Urls__c.getInstance().Opened_Accounts_Url__c,
        'Data should be populated after test setup');

So, make sure you actually insert the custom setting before trying to run the rest of your test. This is a great use case for @TestSetup.

static void setupCustomSetting()
    // insert custom setting here

Note from the Apex Developer Guide:

Isolation of Test Data from Organization Data in Unit Tests

Starting with Apex code saved using Salesforce API version 24.0 and later, test methods don’t have access by default to pre-existing data in the organization, such as standard objects, custom objects, and custom settings data, and can only access data that they create. However, objects that are used to manage your organization or metadata objects can still be accessed in your tests such as:

  • User
  • Profile
  • Organization
  • AsyncApexJob
  • CronTrigger
  • RecordType
  • ApexClass
  • ApexTrigger
  • ApexComponent
  • ApexPage
  • But not having a Hierarchy Custom setting will return a new Custom setting so it will not DNO. "If no custom setting data is defined for the user, this method returns a new custom setting object" – Eric Nov 16 '16 at 17:10
  • But the Opened_Accounts_Url__c on that record will be null. Indeed, if the setting itself were null, that would be a different error entirely. – Adrian Larson Nov 16 '16 at 17:10
  • Disregard. In my test I was setting Pagereference(null) which was different error (Ambiguous signature) from String t; New pagereference(t); in signatures. Should have replicated exactly....Latter does throw Arg 1 cannot be null – Eric Nov 16 '16 at 17:13
  • @AdrianLarson Thank you for your explanation, I added this code: ' External_Urls__c instance = External_Urls__c.getInstance( Opened_Accounts_Url__c='Populated'" before my Test.StartTest(); and I'm getting an error message that the variable Opened_Accounts_Url__c doesn't exist. Could you tell me what I'm missing? Thank you – The Ghost Nov 16 '16 at 18:56
  • @Ghost fixed the syntax in my post. – Adrian Larson Nov 16 '16 at 19:00

It is highly likely that the custom setting values for External_Urls__c has not been defined within this sandbox. Please verify that this is a valid value. If not you will need to create these values in your unit test to have the test pass successfully.

  • The custom setting exists and I created a button for it, it is calling a visualforce page and everything is working fine on the Sandbox, the problem is when I write the test classes.. I can't deploy it to prod. :( – The Ghost Nov 16 '16 at 16:38
  • When you say the custom setting exists you mean the value exists in salesforce but does not exist in your test execution due to test data isolation. You are required to manually create this custom setting value in the context of your unit test. That is why the test is failing.. – Benjamin Pirih Nov 16 '16 at 18:48
  • Yes, I understood that after Adrian explained it. Thanks! – The Ghost Nov 16 '16 at 18:53

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.