2

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("Passing empty string as second parameter")
    public void test_definition(DataTable dataTable) {
        List<Map<String, String>> 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=}]?
    }
makozaki
  • 2,315
  • 1
  • 5
  • 27

1 Answers1

6

Since v5.0.0 empty cells in a data table are into null values rather than the empty string. By using replaceWithEmptyString = "[blank]" 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("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;
}

Note that you don't need to use

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

You can use:

public void test_definition(List<Map<String, String>> 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

M.P. Korstanje
  • 6,760
  • 3
  • 28
  • 43