英文:
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("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=}]?
}
答案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 = "[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) {
答案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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论