将空字符串传递给cucumber DataTable

huangapple go评论87阅读模式
英文:

Pass empty string to cucumber DataTable

问题

  场景: 测试带有空字符串的数据表
作为空字符串作为第二个参数传递时
      | 第一列  | 第二列 |
      | 简单    |        |
@当("作为空字符串作为第二个参数传递时")
    public void 测试定义(DataTable dataTable) {
        List<Map<String, String>> maps = dataTable.asMaps(String.class, String.class);
        System.out.println(maps);
// 这将导致 [{第一列=简单, 第二列=null}]
// 有没有办法传递空字符串并实现 [{第一列=简单, 第二列=}]?
    }
英文:

I'm updating my cucumber version from old info.cukes to latest io.cucumber (v6.8.1) and some of my tests are failing due to change of transformer behavior.
Passing empty strings used to be possible but now they are transformed to null. How can I pass empty string to DataTable argument?

  Scenario: Testing datatable with empty string
    When Passing empty string as second parameter
      | first  | second |
      | simple |        |
@When(&quot;Passing empty string as second parameter&quot;)
    public void test_definition(DataTable dataTable) {
        List&lt;Map&lt;String, String&gt;&gt; maps = dataTable.asMaps(String.class, String.class);
        System.out.println(maps);
// this results in [{first=simple, second=null}]
// is there a way to pass empty string and achieve [{first=simple, second=}]?
    }

答案1

得分: 15

自 v5.0.0 版本开始,数据表中的空单元格被转换为 null 值,而不再是空字符串。通过在数据表类型上使用 replaceWithEmptyString = "[blank]",可以显式地插入空字符串。

这使得以下操作成为可能:

Feature: Whitespace
  Scenario: Whitespace in a table
   Given a blank value
     | key | value   |
     | a   | [blank] |
@given("A blank value")
public void givenABlankValue(Map<String, String> map){
    // map contains { "key":"a", "value": ""}
}

@DataTableType(replaceWithEmptyString = "[blank]")
public String stringType(String cell) {
    return cell;
}

请注意,您无需使用

List<Map<String, String>> maps = dataTable.asMaps(String.class, String.class);

您可以使用:

public void test_definition(List<Map<String, String>> maps) {

另请参阅:https://github.com/cucumber/cucumber-jvm/blob/main/release-notes/v5.1.0-v5.7.0.md#v520-redefine-build-in-data-table-types

英文:

Since v5.0.0 empty cells in a data table are into null values rather than the empty string. By using replaceWithEmptyString = &quot;[blank]&quot; on a datatable type an empty string can be explicitly inserted.

This enables the following:

Feature: Whitespace
  Scenario: Whitespace in a table
   Given a blank value
     | key | value   |
     | a   | [blank] |

@given(&quot;A blank value&quot;)
public void givenABlankValue(Map&lt;String, String&gt; map){
    // map contains { &quot;key&quot;:&quot;a&quot;, &quot;value&quot;: &quot;&quot;}
}

@DataTableType(replaceWithEmptyString = &quot;[blank]&quot;)
public String stringType(String cell) {
    return cell;
}

Note that you don't need to use

List&lt;Map&lt;String, String&gt;&gt; maps = dataTable.asMaps(String.class, String.class);

You can use:

public void test_definition(List&lt;Map&lt;String, String&gt;&gt; maps) {

See also: https://github.com/cucumber/cucumber-jvm/blob/main/release-notes/v5.1.0-v5.7.0.md#v520-redefine-build-in-data-table-types

答案2

得分: 1

我也遇到了类似的问题,而且有太多的情况需要在多个地方进行这些更改。所以我选择了以下更改,使其像以前一样工作:

@DataTableType
public String nullToString(String cell) {
  return Objects.isNull(cell) ? StringUtils.EMPTY : cell;
}
英文:

I too was facing similar issues, and had too many scenarios to make these changes at multiple places. So I opted, below change to make it work as before:

@DataTableType
  public String nullToString(String cell) {
    return Objects.isNull(cell) ? StringUtils.EMPTY : cell;
  }

huangapple
  • 本文由 发表于 2020年10月19日 20:09:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64427058.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定