2014-11-21 142 views
0

我用黃瓜進行BDD和一個功能有這種類型的數據表,我怎麼能在黃瓜生成的方法區分哪個類應該用於黃瓜的DataTable有兩種不同類型的

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。當使用列表時,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; 
    } 
} 

還有其他建議嗎?

回答

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玩具與正在使用中的表頭,以便黃瓜知道相同的屬性名,其域填充:

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

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

黃瓜現在應該有玩具的爲條款「與下面的一組玩具的」填充的列表打電話給你的方法。

相關問題