javaserenity-bddcucumber-serenity

Issue accessing env specific parameters from serenity.conf from a static context


In cucumber if you use the BeforeAll annotation to run before all of your test code there is a requirement to have the method set as static. I set up such a method that I wanted to call a method to set the environment variables taken from the serenity.conf file:

@BeforeAll
public static void beforeAllTests() {
    setEnvVars();
}

This needs to be done to set up all test users to use per environment. The serenity.conf file has an environments section that gives me environment variables per environment that can be called via maven with

mvn clean verify -Denvironment=stage

When I access the environment specific variables via this static beforeall method I get a nullPointerException on the configuration line as the environmentVariables is not initiated. Here is the static method setEnvVars:

public EnvironmentVariables environmentVariables;
public static String MY_ENV_VAR;

public static void setEnvVars() {
    EnvironmentSpecificConfiguration configuration = EnvironmentSpecificConfiguration.from(environmentVariables);
    MY_ENV_VAR = configuration.getProperty("property.from.serenity.conf.file");
    }

Now if I use the @Before cucumber annotation, which runs before each test, I dont have to use static and the above code works without exceptions. I was wondering if its possible to access system resources from the serenity.conf file from a static method. I need to instantiate the user list for all tests before I call code to get users for each tests in the before methods. Any help would be greatly appreciated

A


Solution

  • I got an answer from John Ferguson Smart on this issue and he said to use

    SerenityInfrastructure.getEnvironmentVariables()
    

    This does not seem to be available before serenity version 3.98