How to convert a DataTable in Cucumber to a List of objects?(如何将 Cucumber 中的 DataTable 转换为对象列表?)
问题描述
Original Title: What does scalar mean in Cucumber DataTables in Java?
From this reference:
Java provides several scalar types. These include primitive numeric types, plus boolean and char.
Every scalar (primitive) type has an associated wrapper class or reference type.
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);
}
However, I was able to pass String.class in asList():
List<String> list = dataTable.asList(String.class);
A String is not a primitive in Java. I would like some clarification on what "scalar" means in this context.
I did not find an explicit definition about what Cucumber for Java means with scalar type.
The best hint I could find was in the snippet that is produced for new steps that accept a DataTable. The generated comment reads:
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)
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.
Actually, a short test showed that I can use any type that has a constructor with a single String as parameter.
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 |
这篇关于如何将 Cucumber 中的 DataTable 转换为对象列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将 Cucumber 中的 DataTable 转换为对象列表?
 
				
         
 
            
        基础教程推荐
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 从 python 访问 JVM 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 大摇大摆的枚举 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
						 
						 
						 
						 
						 
				 
				 
				 
				