Search code examples
javaspringcucumberintegration-testing

How to initialize environment variable in a Spring-Java integration test using Cucumber?


I added the following environment variable to my project's properties file (src/test/resources/application.properties):

#variables
  limit=5

And I want to develop an integration test but it always goes wrong because the value of my limit variable is 0 instead of 5 (which was the value I set in properties). I tried using @SpringBootTest and @TestPropertySource but I was unsuccessful, the variable remains as zero:

@RunWith(Cucumber.class)
@SpringBootTest(properties = {"limit=5"})
@CucumberOptions(features = { "classpath:project/feature/list" }, plugin = {"pretty" }, monochrome = true)
public class RunCucumberTestIT {

}

@RunWith(Cucumber.class)
@TestPropertySource(properties = {"limit=5"})
@CucumberOptions(features = { "classpath:project/feature/list" }, plugin = {"pretty" }, monochrome = true)
public class RunCucumberTestIT {

}

Solution

  • Thanks for everyone who tried to help. I will share how I managed to solve it, I didn't actually need to use @TestPropertySource or @SpringBootTest, I just changed the way my variable was declared within the service that my integrated test calls.

    Before:

    @Value("${limit}")
    private static int dateFilterLimit;
    

    After:

    @Value("${limit:5}")
    private int dateFilterLimit;
    

    Thus, if the test fails to see what is declared in the properties, the value 5 is set within the variable. For the rest of the code, the variable in properties remains:

    #variables
    limit=5
    

    And the configuration class looks like this:

    @RunWith(Cucumber.class)
    @CucumberOptions(features = { "classpath:project/feature/list" }, plugin = {"pretty" }, monochrome = true)
    public class RunCucumberTestIT {
    
    }