SpringBoot: Making use of @Value properties from YAML files for Cucumber tests

In case you use:
1. SpringBoot to build your web applications using Java
2. Test it with Cucumber
3. Use YAML files for configurable property values

You can use the YamlPropertiesFactoryBean to make use of @Value for your test configuration.

application.yml:


myapp:
  base:
    url: some_url

cucumber.xml (or application context .xml):


<context:component-scan base-package="cucumber.runtime.java.spring"/>
    <context:annotation-config/>

    <bean id="yamlProperties"
          class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean">
        <property name="resources" value="classpath:application.yml"/>
    </bean>

    <context:property-placeholder properties-ref="yamlProperties"/>

Java code (snipped):


@ContextConfiguration(locations = {"classpath:cucumber.xml"})
public class TestSteps {

    @Value("${myapp.base.url}")
    private String baseUrl;
...

The spring bean, yamlProperties, will take care of all the necessary wiring, so all you have to do is make use of the @Value annotation. The benefit of this is you can create properties for various environments (in your YAML file) and be able to automate running your tests against any environment using spring profiles.

Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.