Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass empty string to cucumber DataTable

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=}]?
    }
like image 364
makozaki Avatar asked Mar 26 '26 04:03

makozaki


2 Answers

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

like image 90
M.P. Korstanje Avatar answered Mar 28 '26 16:03

M.P. Korstanje


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;
  }
like image 21
Deepak Thorecha Avatar answered Mar 28 '26 17:03

Deepak Thorecha