5

Currently in our project's tests we switched from using annotated step definitions, e.g.:

public class Steps {
    @Given("^Step name$")
    void methodName() { 
        // do sth
    }
}

to lambda expressions:

public class Steps implements En {
    public Steps() {
        Given("^Step name$", () -> 
            // do sth
        );
    }
}

When using Intellij Cucumber Java plugin it was easy to find the usage of some step, since it looked for usages of the annotated method (I presume). Now however, we have to manually search for the regex passed as the argument.

My first question is: is there a neat way to do this with lambda expressions?

Moreover: when using Intellij's tool for version control and commiting files containing definitions of big numbers of steps, the code analysis tool goes on forever (I guess it is because of the constructor having to crank a lot of code).

So the second question is: since there is no possibility of the step library shrinking and step usage search is used very often wouldn't it be a good idea to switch back to Ye Olde Way i.e. using annotated methods?

Vinay Prajapati
  • 7,199
  • 9
  • 45
  • 86
DCzo
  • 141
  • 2
  • 14
  • Can you explain, why you changed the code? As far as we can see, the actual implementation (`// do sth`) is still the same, the declaration around it has become bigger and you’re running into the problems you’re describing in your question. Is there any *advantage* justifying these disadvantages? – Holger Mar 08 '18 at 09:03
  • Well I wasn't actually my decision ;). I however have to deal with the outcome. I was hoping that if there is an easy solution (which I wasn't able to find) I could implement it. If not, that's another case for reverting the changes. – DCzo Mar 08 '18 at 09:09
  • Well, perhaps someone with experience with the IntelliJ Cucumber Java plugin will show up… – Holger Mar 08 '18 at 09:13
  • Regarding long running analysys, would be great if you can report the issue at https://youtrack.jetbrains.com/issues/IDEA with IDE [log](https://intellij-support.jetbrains.com/hc/en-us/articles/207241085) folder zipped and [CPU snapshots](https://intellij-support.jetbrains.com/hc/en-us/articles/207241235) taken when analysys is running. – Andrey Mar 08 '18 at 15:17
  • Will do, thank you for your response. – DCzo Mar 09 '18 at 08:58

2 Answers2

1

Find usages for java-8 style step definitions do not yet work. One can vote and follow this request: IDEA-144648.

Andrey
  • 15,144
  • 25
  • 91
  • 187
  • Could you please mention which version of IntelliJ / Cucumber plugin you use and lead to the problem? As the bug was reported in 2015 it might be already fixed. With the recent versions it works for me as expected. – SubOptimal Jun 20 '18 at 12:07
  • Currently mentioned IDEA-144648 issue is planned for the fix in 2018.2 version. – Andrey Jun 20 '18 at 20:27
  • I believe this is always the next release as one line up it states `Fixed in builds No Fixed in build`. As I wrote (see my answer) it is already working as expected. – SubOptimal Jun 21 '18 at 05:33
  • The state of the issue [IDEA-144648](https://youtrack.jetbrains.com/issue/IDEA-144648) has was recently updated to `Obsolete`. – SubOptimal Jun 26 '18 at 11:11
0

It is working here while using

  • IntelliJ IDEA 2018.1.5
  • 'Cucumber for Java' plugin 181.5281.24

pom.xml

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <version.cucumber>3.0.2</version.cucumber>
    <!-- following versions were also checked -->
    <!--<version.cucumber>2.4.0</version.cucumber>-->
    <!--<version.cucumber>2.3.1</version.cucumber>-->
</properties>
<dependencies>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java8</artifactId>
        <version>${version.cucumber}</version>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>${version.cucumber}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

userdata.feature

Feature: demo for java8 glue classes

  Scenario: user login on home page
    Given user is on home page
    When user navigate to login page
    And user enters credentials to login
    Then message displayed login successfully

glue/StepPojo.java

package glue;

import cucumber.api.PendingException;
import cucumber.api.java8.En;

public class StepPojo implements En {
    public StepPojo() {
        Given("^User is on Home Page$", () -> {
            throw new PendingException();
        });
        When("^User Navigate to LogIn Page$", () -> {
            // Write code here that turns the phrase above into concrete actions
            throw new PendingException();
        });
        And("^User enters Credentials to LogIn$", () -> {
            // Write code here that turns the phrase above into concrete actions
            throw new PendingException();
        });
        Then("^Message displayed Login Successfully$", () -> {
            // Write code here that turns the phrase above into concrete actions
            throw new PendingException();
        });
    }
}

The class StepPojo.java was created by the Cucumber plugin by pressing ALT+ENTER while selecting a step in the feature file.

The feature file was shown before as

before defining the glue methods

after the steps are defined as

[after defining the glue methods[2]

when you hover with the mouse over a step while pressing down the CTRL key it looks as

hover over step

when you click on a step while pressing down the CTRL key you jump to the respective method, e.g.

Given("^User is on Home Page$", () -> {
    throw new PendingException();
});
SubOptimal
  • 22,518
  • 3
  • 53
  • 69
  • Actually that wasn't the initial issue: the problem was finding the usages of step definitions i.e. doing the reverse of what you demonstrated ;). In the pre-java8 version, the step definition was simply a method, therefore when you would CTRL-click on the method name it would show you a list of places in the .feature files where this step was used. However, that was not the case for lambdas, for which nothing happened. – DCzo Jun 29 '18 at 11:25
  • @DCzo Obviously I misread your question. I did not even know it was possible with `cucumber-java`. Nice to know. – SubOptimal Jul 02 '18 at 05:42