如何将黄瓜中的数据表转换为对象列表? [英] How to convert a DataTable in Cucumber to a List of objects?

查看:60
本文介绍了如何将黄瓜中的数据表转换为对象列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自参考:


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

Java provides several scalar types. These include primitive numeric types, plus boolean and char.

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

Every scalar (primitive) type has an associated wrapper class or reference type.

阅读javadocs:

Reading the 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);
}

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

However, I was able to pass String.class in asList():

List<String> list = dataTable.asList(String.class);

字符串不是Java中的基元。我想澄清一下标量在这种情况下的含义。

A String is not a primitive in Java. I would like some clarification on what "scalar" means in this context.

推荐答案

我没有找到关于黄瓜的明确定义。对于Java,意味着具有标量类型

I did not find an explicit definition about what Cucumber for Java means with scalar type.

我能找到的最佳提示是在为new生成的代码段中接受 DataTable 的步骤。生成的注释为:

The best hint I could find was in the snippet that is produced for new steps that accept a DataTable. The generated comment reads:


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

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)

除了 Java标量类型( byte short int char 布尔值或它们的包装器类型 Byte Short Integer Long Char Boolean ),您也可以使用 String java.util.Date 和枚举类型。

So it seems that besides the "Java scalar types" (byte, short, int, long, char, boolean, or respectively their wrapper types Byte, Short, Integer, Long, Char and Boolean) you can also use String, java.util.Date and enum types.

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

Actually, a short test showed that I can use any type that has a constructor with a single String as parameter.

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

A small example with my own value class (very contrived). The output from the following snippets is a 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 |

这篇关于如何将黄瓜中的数据表转换为对象列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆