黄瓜DataTable有两个不同的类

时间:2014-11-21 16:40:14

标签: java datatable cucumber bdd

我使用黄瓜进行BDD并且在一个功能中有这种类型的数据表,如何区分黄瓜生成的方法哪个类应该使用

And the following set of "Toys"
    | name           |something1| 
    | plane          |  400     |  
    | ball           |  800     |  
 And the following set of "Shoes"
    | name           | something2| 
    | boots          |   35      | 
    | sandals        |   35      | 

此实现抛出异常无法将表转换为java.util.List。使用List时,SomeComplexType不能是泛型类型

@Given("^the following set of \"([^\"]*)\"$")
public <T> void the_following_set_of(String type, List<T> info) throws Throwable {
    switch (type) {
    case "Toys":            

        break;
    case "Shoes":
        break;
    default:
        break;
    }
}

还有其他建议吗?

1 个答案:

答案 0 :(得分:2)

如果类型也是参数,您将无法自动转换为类型对象列表,因为Cucumber在调用定义时不知道它应该转换为哪种类型。您必须使用上面的代码并手动进行转换。如果您有每种类型的方法,黄瓜会为您进行转换,例如:

@When("^the following set of toys")
public void the_following_set_of(List<Toy> toys) throws Throwable {
    System.out.println(toys);
}
// ... another method for shoes, etc.

然后,只需为您的表标题中使用相同属性名称定义一个简单的玩具bean,以便Cucumber知道要填充哪些字段:

public class Toy {
    private String name;
    private String something1;

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    // ... etc.
}

Cucumber现在应该使用填充的玩具列表来调用您的方法,用于条款&#34;以及以下一组玩具&#34;。