Select Page
QA Automation

Cucumber Lambda expressions

As we all know, jvm-Cucumber has introduced maven dependency for Java 8. With the new API, you can write your step definitions with lambda expressions.

Cucumber Lambda expressions
Listen to this blog

As we all know, jvm-Cucumber has introduced maven dependency for Java 8. With the new API, you can write your step definitions with lambda expressions. It is very easy to setup and creates new step definitions for your feature using Lambda expressions, but understanding Lambda expression is vital.

What is Lambda Expression?

Are you able to create a Java function without a name and belonging to any class? If anyone says “Yes” then it can be done only with Lambda Expression. A lambda expression is an anonymous function and nameless.

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.

Given("I login as (.*)$",(String name)-> System.out.println(name));
  
How to setup?

Step 1: Add Cucumber Java 8 Maven Dependency

<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java8</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>

Step 2: Add Maven compiler plugin

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>  

That’s it.

Step Definition with Lambda Expression
package steps;

import cucumber.api.java8.En;

public class MyStepdefs implements En {

    public MyStepdefs() {
       Given("I login as (.*)$",(String name)-&gt; System.out.println(name));
    }
}
  

Comments(0)

Submit a Comment

Your email address will not be published. Required fields are marked *

Talk to our Experts

Amazing clients who
trust us


poloatto
ABB
polaris
ooredo
stryker
mobility