Cucumber test using Lambda expressions

Now let us write sample cucumber step definitions using Lambda expressions.

Here is the sample feature file.

Feature: Lambda feature

  Scenario: Verify sum
    Given I add 1 and 2
    Then I verify that sum is 3

Here is the sample Test Class using Lambda expressions to write the step definitions. Note that StepClass must implement cucmber.api.java8.En interface and step methods should be inside the constructor of test class.  You also need to ensure that Java language level should be 1.8.

package org.softpost;


import cucumber.api.java8.En;

public class StepClass8 implements En {

    public StepClass8() {

        Given("^I add 1 and 2$", () -> {
            System.out.println("Adding 1 and 2");
        });

        Then("^I verify that sum is 3$", () -> {
            System.out.println("Checking that sum is 3");
        });

    }

}

If you are using Maven, you can specify the Java language level using below XML section in POM.XML

<build>
        <plugins>
            <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>
        </plugins>
    </build>

You may also like...