如何将黄瓜中的数据表转换为对象列表?

sil*_*ver 1 java cucumber

原标题:标量在Java的Cucumber DataTables中意味着什么?

这个参考:

Java提供了几种标量类型。这些包括原始数字类型,以及布尔值和char。

每个标量(原始)类型都有一个关联的包装器类或引用类型。

阅读javadocs:

/**
  * Converts the table to a List.
  *
  * If {@code itemType} is a scalar type the table is flattened.
  *
  * Otherwise, the top row is used to name the fields/properties and the remaining
  * rows are turned into list items.
  *
  * @param itemType the type of the list items
  * @param <T>      the type of the list items
  * @return a List of objects
  */
public <T> List<T> asList(Class<T> itemType) {
    return tableConverter.toList(this, itemType);
}

/**
  * Converts the table to a List of List of scalar.
  *
  * @param itemType the type of the list items
  * @param <T>      the type of the list items
  * @return a List of List of objects
  */
public <T> List<List<T>>> asLists(Class<T> itemType) {
    return tableConverter.toLists(this, itemType);
}
Run Code Online (Sandbox Code Playgroud)

但是,我能够在asList()中传递String.class:

List<String> list = dataTable.asList(String.class);
Run Code Online (Sandbox Code Playgroud)

字符串不是Java中的基元。我想澄清一下在这种情况下“标量”是什么意思。

sil*_*ver 7

引用生成的代码片段:

// For automatic transformation, change DataTable to one of
// List<YourType>, List<List<E>>, List<Map<K,V>> or Map<K,V>.
// E,K,V must be a scalar (String, Integer, Date, enum etc)
Run Code Online (Sandbox Code Playgroud)

黄瓜javadocs中的“标量”可能已被错误地用来集体表示原语(即标量)的关联包装类


从下面的示例开始,asList()按照docs创建用户定义的Expense对象的列表:

否则,第一行用于命名字段/属性,其余的行变成列表项。

我观察到以下内容:

  1. Feature文件中的第一行(标题)必须对象的字段名称匹配
  2. 构造函数可以接受所有字段作为参数。

用户定义的对象(非标量):

public class Expense {

    private String name = null;
    private String amount = null;
    private String frequency = null;

    public Expense(String name, String amount, String frequency) {
        this.name = name;
        this.amount = amount;
        this.frequency = frequency;
    }

    // Getters and setters
}
Run Code Online (Sandbox Code Playgroud)

特征:

When I Enter My Regular Expenses
  | name        | amount | frequency     |
  | Electricity |   5500 | Monthly       |
  | Water       |    900 | Weekly        |
  | Internet    |   1900 | Every 2 Weeks |
  | Cable TV    |    555 | Daily         |
Run Code Online (Sandbox Code Playgroud)

步骤Def:

@When("^I Enter My Regular Expenses$")
public void I_Enter_My_Regular_Expenses(DataTable dataTable) throws Throwable {
  List<Expense> expenseList = dataTable.asList(Expense.class);

  for (Expense expense : expenseList) {
    System.out.println(expense);
  }

  // Here, asList() creates a List of Expense objects.
}
Run Code Online (Sandbox Code Playgroud)

输出:

输出

  • 仅适用于 Cucumber-JVM 2.4.0。单击 [此处](/sf/ask/3554029951/) 获取 v3.xx (2认同)

Tho*_*ger 1

我没有找到关于 Cucumber for Java 的明确定义scalar type

我能找到的最好的提示是在为接受DataTable. 生成的评论如下:

对于自动转换,请将 DataTable 更改为 List<YourType>、List<List<E>>、List<Map<K,V>> 或 Map<K,V> 之一。E、K、V 必须是标量(字符串、整数、日期、枚举等)

因此,似乎除了“Java 标量类型”(byteshortintlongcharboolean或分别是它们的包装类型ByteShortIntegerLong和)之外CharBoolean您还可以使用String,java.util.Date和枚举类型。

实际上,一个简短的测试表明我可以使用任何具有带有单个String参数的构造函数的类型。


一个带有我自己的价值类别的小例子(非常做作)。以下代码片段的输出是一个List<List<MyValueClass>>.

// MyValueClass.java
public class MyValueClass {

    private final String value;

    public MyValueClass(String v) {
        this.value = v;
    }

    public String getValue() {
        return value;
    }
}

// snippet from MySteps.java
@Given("^a table with$")
public void a_table_with(DataTable arg1) throws Throwable {
    System.out.println(arg1.asLists(MyValueClass.class));
}

// snippet from my test1.feature
  Scenario: Test with Datatable
    Given a table with
      | a | b | c |
      | 1 | 2 | 3 |
      | a | b | c |
Run Code Online (Sandbox Code Playgroud)