使用空 List 参数在 Gherkin 中创建 Cucumber 特征

Create a Cucumber feature in Gherkin with an empty List argument

我在 Java 中使用 Cucumber,并编写了一个以 List 作为参数的场景,即

And the following ids are within the range: 1, 2

这对方法定义成功:

@Given("^the following ids are within the range: (.*)$")
public void ids_are_within_range(List<String> idsWithinRange) {
    this.idsWithinRange = idsWithinRange
}

但是,如果我尝试不将任何 ID 传递到列表中,就会遇到问题:

And the following ids are within the range: 

测试根本不执行,告诉我需要执行相关的测试方法。它认为以列表结尾的行实际上是一种不带任何参数的不同方法:

Wrong test finished. Last started: [] stopped: Scenario: Search for manifests, none of which are within a date range in the data platform; class org.junit.runner.Description
Test '.Feature: Restful CRUD service for manifest searches.Scenario: Search for manifests, none of which are within a date range in the data platform' ignored
...
You can implement missing steps with the snippets below:
...
@Given("^and the following ids are within the range:$")

如何将空 List 传递到我的方法中?

作为解决方法(不是一个优雅的解决方案),您可以按如下方式设置步骤定义:

And the following ids are within the range: ,

您将在您的方法中收到一个已初始化的空 ArrayList。 希望能帮助到你。无论如何,我会等待其他答案,也许有人可以提供更好的解决方案。