Cucumber-JVM: Running a @Before step only for certain scenarios

You can make use Cucumber’s @Before and @After to execute only for certain scenarios like the below. This is especially useful if you, say, wanted to make a (Mongo) DB connection for a few test scenarios.

public void beforeScenario() {
  // actions

public void afterScenario() {
  // actions

To complete the hook, you simply add “@mongo” tag to the scenarios in your feature file. Cheers!

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.


    url: some_url

cucumber.xml (or application context .xml):

<context:component-scan base-package=""/>

    <bean id="yamlProperties"
        <property name="resources" value="classpath:application.yml"/>

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

Java code (snipped):

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

    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.

Maven: How to only execute certain Cucumber tags

I needed Maven to only run certain tests that had a specific tag (i.e., @regression).  I couldn’t easily find information via the Cucumber website, so I’m logging it her for archiving.  Note: This command also works in Jenkins via the “Goals and options” section.

clean install -Dcucumber.options="--tags @regression"