Search code examples
javacucumbercucumber-jvmgherkincucumber-java

Cucumber step definition with asterisk


We have feature files that have long tests that validate multiple things written in non-english language, the Given -> When -> Then structure doesn't make sense. I tried replacing feature file keywords with *, and that works just fine, however here are the problems:

  1. When writing a new step in a form of * Some step, and using Alt->Enter shortcut to generate a step definition, IntelliJ IDEA does... Nothing. It only opens the file where I wanted to put the definition without any added code. I've updated IDE and plugins to be latest.
  2. There doesn't seem to be any way to have a "universal" annotation to use for asterisk steps, only Given, When, Then, And, But exists. It's not very logical to have a * Some step feature and @Given("Some step") definition.

Is there any workaround that I might use?


Solution

  • I think you can achieve this with few coding steps.

    1. Say you have the scenario like this:
    Feature: Generic annotation
    
      Scenario: Testing annotations
        * having asterix
        * use custom generic annotation
    
    1. Add custom annotation to your sources
    package click.webelement.cucumber;
    
    import io.cucumber.java.StepDefinitionAnnotation;
    import io.cucumber.java.StepDefinitionAnnotations;
    import org.apiguardian.api.API;
    import java.lang.annotation.*;
    
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    @StepDefinitionAnnotation
    @Documented
    @Repeatable(MyStep.MySteps.class)
    @API(status = API.Status.STABLE)
    public @interface MyStep {
    
        String value();
    
        @Target(ElementType.METHOD)
        @Retention(RetentionPolicy.RUNTIME)
        @StepDefinitionAnnotations
        @Documented
        @interface MySteps {
            MyStep[] value();
        }
    }
    
    1. Now use it in your Step Definition
    package click.webelement.cucumber;
    
    public class StepDef {
    
        @MyStep("having asterix")
        public void doOne(){
            System.out.println("Running having asterix");
        }
    
        @MyStep("use custom generic annotation")
        public void doTwo(){
            System.out.println("Running use custom generic annotation");
        }
    
    }
    

    UPD

    In order to make your Idea plugin work with your custom annotation you need to place that annotation to

    io.cucumber.java.LANG package where LANG is the sub-package for your chosen language.

    To make everything work as default you place it to

    io.cucumber.java.en