What is the proper way to load local.settings.json properties in a Java Azure function app unit test?

What is the proper way to load local.settings.json properties in a Java Azure function app unit test?

When I run unit tests, the tests don't pick up the settings file configurations automatically, like it does when I just run the function app directly.

This is how I did it, but I fear it is a hack? I could find no documentation on how to do this. For all I know, there is an existing class in the Java SDK that already knows how to handle this?

public class ConfigurationClientProvider {

    private static final String CONNECTION_STRING = "APP_CONFIGURATION_CONNECTION_STRING";

    private static final String LOCAL_SETTINGS_FILE = "local.settings.json";

    public ConfigurationClient createConfigurationClient() {
        return new ConfigurationClientBuilder()
                .connectionString(getConnectionString())
                .buildClient();
    }

    public String getConnectionString() {
        Optional<String> optionalConnectionString = Optional.ofNullable(System.getenv(CONNECTION_STRING));
        return optionalConnectionString.orElse(readPropertyFromLocalConfig(CONNECTION_STRING, "Values"));
    }

    public String readPropertyFromLocalConfig(String propertyName, String parentKey) {
        try (FileInputStream fis = new FileInputStream("local.settings.json")) {
            String data = IOUtils.toString(fis, "UTF-8");
            try {
                JSONObject jsonObject = new JSONObject(data);
                return (String)((JSONObject)jsonObject.get(parentKey)).get(propertyName);
            } catch (JSONException err){
                log.error("Error reading file: " + LOCAL_SETTINGS_FILE, err.toString());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return StringUtil.EMPTY_STRING;
    }

}

Here is how I implemented the readPropertyFromLocalConfig method:

@Slf4j
public class ConfigurationClientProvider {
    private static final String CONNECTION_STRING = "APP_CONFIGURATION_CONNECTION_STRING";
    private static final String LOCAL_SETTINGS_FILE = "local.settings.json";
    private static final String ENV_SETTINGS_KEY = "Values";
    private static final String ENCODING = "UTF-8";

    public ConfigurationClient createConfigurationClient() {
        return new ConfigurationClientBuilder()
                .connectionString(getConnectionString())
                .buildClient();
    }

    public String getConnectionString() {
        return getProperty(CONNECTION_STRING, ENV_SETTINGS_KEY);
    }

    public String getProperty(String propertyName, String parentKey) {
        Optional<String> optionalParentKey = Optional.ofNullable(parentKey);
        Optional<String> optionalConnectionString = Optional.ofNullable(System.getenv(propertyName));
        return optionalConnectionString.orElse(readPropertyFromLocalConfig(propertyName, optionalParentKey.orElse(ENV_SETTINGS_KEY)));
    }

    public String getProperty(String propertyName) {
        Optional<String> optionalConnectionString = Optional.ofNullable(System.getenv(propertyName));
        return optionalConnectionString.orElse(readPropertyFromLocalConfig(propertyName, ENV_SETTINGS_KEY));
    }

    public String readPropertyFromLocalConfig(String propertyName, String parentKey) {
        try (FileInputStream fis = new FileInputStream(LOCAL_SETTINGS_FILE)) {
            String data = IOUtils.toString(fis, ENCODING);
            try {
                JSONObject jsonObject = new JSONObject(data);
                return (String)((JSONObject)jsonObject.get(parentKey)).get(propertyName);
            } catch (JSONException err){
                log.error("Error reading file: " + LOCAL_SETTINGS_FILE, err.toString());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return StringUtil.EMPTY_STRING;
    }

}

If you want to read the local.settings.json file in your local environment for testing purposes you can use the following

//create a JSON parser and Read the file using FileReader

JSONParser jsonparser = new JSONParser();
FileReader localSettingsReader = new FileReader(".\\local.settings.json");

//parse the Read values into object convert the object into Jsonobject

Object obj = jsonparser.parse(localSettingsReader);
JSONObject localsettingsobject = (JSONObject)obj;
String value = (String) localsettingsobj.get("YourAppSettingKeyValue");

Anyway, you already using the same way to load and read the local.settings.json file AppSettings value.

But if you want to check your app settings value when the app is deployed in azure you have to follow the below steps (which is used for both local and azure). Use System.getenv("YourAppSettingKeyValue") to view the app settings value.

public class Function {
    public String getAppSettingsValue(@HttpTrigger(name = "req", methods = {HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) String req, ExecutionContext context) {
        context.getLogger().info("My app setting value: "+ System.getenv("YourAppSettingKeyValue"));
        return String.format(req);
    }
}

Refer here