Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Lambda Expression support for Cucumber Java8

Girija Prasad Panda edited this page Dec 22, 2017 · 2 revisions

Lambda Expression support for Cucumber-Java8

  • Release Version : 0.0.20
  • As we all know, with the new Cucumber-Java8 API, we can write our step definitions with lambda expressions. So cucumbe-eclipse plugin is also enhanced to support both Content-Assistance and Glue-code features for Lambda Expressions.

Features supported for Lambda Expression:

  • Content-Assistance populates all steps used in lambda expressions.
  • Matching of Glue-code for lambda-expression-steps in a feature file
  • Pressing 'F3' key on steps in feature file redirects to the corresponding lambda-expression steps.

Test Environment:

  • JDK/JRE-8
  • Eclipse Java EE IDE for Web Developers. Version: Mars.2 Release (4.5.2) Build id: 20160218-0600

What is Lambda-Expression?

  • A lambda expression is an anonymous function and nameless. Means by Lambda Expression we can create a Java function without any name and belonging to any class. See the below step definition. The Lambda expression is passed as a parameter in Given method. By introducing Lambda expression in Java 8, Java language has stepped into functional programming.

How to use Lambda Expression for Step-definitions?

  • As lambda-expression supported by java8, You must have to use eclipse with JDK/JRE-8 version.
  • If you want to write any step-definitons by lambda-expression, You must have to import cucumber-java8 api(ex.cucumber.api.java8.En) in your step-definition(java) file.
  • You may wrtie step-definitions by using lambda-expression with both constructors and methods.
  • All lambda-expression-steps can be populated in feature-file by Content-Assistance features
  • Always use 'Ctrl+shift+o' to avoid unused import statements in your step-definition(java) file.

Sample Cucumber-Java8 Lambda Expression:

package foo;

import cucumber.api.java8.En;

public class TestLambdaStepdefs implements En {

    //Lambda-steps inside Constructors
    public TestLambdaStepdefs() {
        Given("I have (\\d+) cukes in my belly", (Integer cukes) -> {
            System.out.format("Cukes: %n\n", cukes);
        });
		
	Then("I have a some step definition", () -> {
             throw new Exception();
        });	
		
	Given("testlambda", () -> {
	     System.out.println("Inside Given");
	});

	When("^the search phrase \"([^\"]*)\" is entered$", (String phrase) -> {
		System.out.println("Inside When");
	});

	Then("^results for \"([^\"]*)\" are shown$", (String phrase) -> {
		System.out.println("Inside Then");
	});
    }
	
  //Lambda-steps inside method
  private void testLambdaSteps() {
        Before((Scenario scenario) -> {
            log.info("Before scenario : " + scenario.getName());
        });

        After((Scenario scenario) -> {
            log.info("After scenario : " + scenario.getName());
        });

        Given("^this pre condition$", () -> {
            testContext.put("some-key", "some-value");
        });

        When("^I do this$", () -> {
        });

        When("^I do that$", () -> {
        });

        Then("^I can verify that$", () -> {
            assert (testContext.get("some-key").get().equals("some-value"));
        });

        Then("^I can also verify that$", () -> {
        });
    }
}

Do Not :

  • Don't try cucumber-java8-lambda-expression-steps in JDK/JRE-7 enviromnent, else it would be error in loading of Feature files in eclipse editor.
  • Don't try to import cucumber-java8 api(ex.cucumber.api.java8.En) in your step-definition(java) file for JDK/JRE-7 enviromnent, else it would be error in loading of Feature files in eclipse editor.

Clone this wiki locally

Morty Proxy This is a proxified and sanitized view of the page, visit original site.