IntelliJ IDEA 2023.3 Help

Create step definitions

In Cucumber, step definitions should be stored in a named package under Test Sources Root. Keeping definitions in the default package might result in an error as IntelliJ IDEA will not be able to locate them.

Create a package for step definitions

  1. Right-click Test Sources Root icon Test Sources Root in the Project tool window and select New | Package.

  2. Give the package a meaningful name and press Enter.

    A package for step definitions in the Project tool window

Add step definitions

If a .feature file refers to a non-existent step, IntelliJ IDEA recognizes and highlights such a step, and provides an intention action that helps create the missing step definition.

  1. Place the caret at a step in your .feature file and press Alt+Enter.

    The list of suggested intention actions opens.

  2. Select Create step definition to create a definition only for one step, or select Create all step definitions to add definitions for all steps in a scenario.

    Creating steps definitions from the feature file
  3. If you already have definition files in your project, the IDE prompts you select to which file you want to add the new step definitions. You can select one of the existing files or create a new one.

  4. If you opt to create a new step definition file, a dialog opens. Name the new file, select the type (Java, Java 8, or Groovy) and specify its location.

    Select Java 8 to use lambda expressions in step definitions or Java to use annotations.

    Note that files with step definitions should be located in a dedicated package. Keeping them in the default package might result in an error.

    Click OK.

    Creating a new file for step definitions
  5. In the selected step definition file that opens in the editor, enter the desired code.

Note the following:

  • To be able to create step definitions in Groovy, the Cucumber for Groovy plugin must be installed and enabled.

  • To be able to use lambda expressions in step definitions (Java 8), the corresponding library must be added to the project.

Make sure that the file with step definitions is located in a dedicated package. Otherwise, you won't be able to use navigation.

Jump to a step definition

  • In a .feature file, keep Ctrl pressed and hover over a step. When it turns into a link, click it to jump to its definition.

    Navigating to step definition
  • Alternatively, place the caret at a step and select Navigate | Declaration or Usages from the main menu or just press Ctrl+B.

Jump to a step from its definition

  • In a file with step definitions, keep Ctrl pressed and hover over a step definition. When it turns into a link, click it to jump to the step.

    Navigating to step from step definition
  • Alternatively, place the caret at the necessary step definition and select Navigate | Declaration or Usages from the main menu or just press Ctrl+B.

Last modified: 15 March 2024