为什么用 lambda 表达式实现 Cucumber stepdef 是一个好习惯?
Why implement cucumber stepdef with lambda expression is a good practice?
正如我们所知,使用新的 Cucumber-Java8 API,我们可以使用 lambda 表达式编写步骤定义。
来自 github-cucumber:
的示例代码
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 表达式的优点是什么?
为什么lambda要写在构造中?
提前感谢您的任何意见。
这是为了在编写 Cucumber 测试时启用 Java 函数式编程风格。阅读有关函数式编程功能的更多信息 here。主要特点是:
- 纯函数
- 无状态
- 无副作用
- 不可变变量
正如我们所知,使用新的 Cucumber-Java8 API,我们可以使用 lambda 表达式编写步骤定义。 来自 github-cucumber:
的示例代码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 表达式的优点是什么? 为什么lambda要写在构造中?
提前感谢您的任何意见。
这是为了在编写 Cucumber 测试时启用 Java 函数式编程风格。阅读有关函数式编程功能的更多信息 here。主要特点是:
- 纯函数
- 无状态
- 无副作用
- 不可变变量