在步骤中访问JBehave示例表数据

时间:2013-10-08 22:02:00

标签: java annotations tdd bdd jbehave

我想知道是否有一种方法可以在步骤方法中访问示例表行数据而不将其作为参数传递?

故事档案:

Given I am logged in
When I create a trade
Then a trade should be created

Examples:
|data1|data2|
|11111|22222|
|33333|44444|

步骤文件:

@When("I create a trade")
public void createTrade(@Named("data1") String data1, @Named("data2") String data2){
    //code to create trade using data1 and data2
}

上面工作正常,但我想从方法中的示例表中访问数据行。 (我想这样做的原因是因为在每个故事的示例表中都可能没有所有列,并且我发现如果我在步骤方法中说3 * @Named作为参数,但其中一个是从实际的示例表中丢失,然后它无法运行。)

@When("I create a trade")
public void createTrade(){
    //check if there is a data1 column, if so get value and do something
    //check if there is a data2 column, if so get value and do something
}

感谢您的帮助

2 个答案:

答案 0 :(得分:2)

您可以实现新的参数转换器,然后将表作为对象传递。 例如,在我们的项目中,我们构建了ExamplesTableConverter(注意:我们没有测试我们的JBehave转换器):

public class ExamplesTableConverter extends ParameterConverters.ExamplesTableConverter {
private final ExamplesTableFactory factory;
private static final String ROW_SEPARATOR = "\n";
private static final String FIELD_SEPARATOR = "|";

public ExamplesTableConverter(){
    this(new ExamplesTableFactory());
}

public ExamplesTableConverter(ExamplesTableFactory factory){
    this.factory = factory;
}

@Override
public boolean accept(Type type) {
    if (type instanceof Class<?>){
        return ExamplesTable.class.isAssignableFrom((Class<?>) type);
    }
    return false;  //To change body of implemented methods use File | Settings | File Templates.
}

@Override
public Object convertValue(String tableAsString, Type type) {
    System.out.println(tableAsString);
    String[] rows = tableAsString.split(ROW_SEPARATOR);
    StringBuffer resultString = new StringBuffer();
    resultString.append(rows[0]);
    resultString.append(ROW_SEPARATOR);

    for(int i=1; i<rows.length; i++){
        String originRow = rows[i];
        List<String> rowValues = TableUtils.parseRow(originRow, FIELD_SEPARATOR, true);

        String translatedRow = translateRow(rowValues);
        resultString.append(translatedRow);
        resultString.append(ROW_SEPARATOR);
    }
    System.out.println(resultString.toString());
    Object table = factory.createExamplesTable(resultString.toString());
    return table;
    //return null; 
}

private String translateRow(List<String> rowValues) {
    StringBuffer result = new StringBuffer(FIELD_SEPARATOR);

    for(String field : rowValues){
        try{
        result.append(translate(field));
        result.append(FIELD_SEPARATOR);}
        catch (LocalizationException e){
            e.printStackTrace();
            //Need do something here to handle exception
        }
    }
    return result.toString(); 
}

}

然后您需要将该转换器添加到您的配置中:

configuration.parameterConverters().addConverters(new ExamplesTableConverter());

创建一个使用它的方法

@When("create parameters of type $param1 from the next table: $table")    
public void doThis(@Named("param1") String param1, @Named("table") ExamplesTable table)

最后,在故事中使用它:

When create parameters of type type1 from the next table:
|FirstName|LastName|
|Donald|Duck|

这将允许您迭代表。

以下博客文章也可以帮到你 Simpler JBehave Examples Table Processing

答案 1 :(得分:0)

场景: -

Given data insert in DemoSheet
 |demoNumber|demoName|
 |101|Demo1|
 |102|Demo2|
 |103|Demo3|
 |104|Demo4|
 |105|Demo5|

用于从场景中获取值的java类: -

@Given("data insert in DemoSheet $parameterTable")
public void setDataToSheet(ExamplesTable parametersTable)
        throws RowsExceededException, WriteException, IOException {

    List<String> demonum = new ArrayList<String>();
    List<String> demoname = new ArrayList<String>();

    for (Map<String, String> row : parametersTable.getRows()) {
        Iterator it = row.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry) it.next();
            if (pairs.getKey().equals("demoNumber")) {
                demonum.add((String) pairs.getValue());
            } else if (pairs.getKey().equals("demoName")) {
                demoname.add((String) pairs.getValue());
            }
        }
    }
    for(String s:demonum)
    {
       System.out.println(s.getDemonum);
       System.out.println(s.getDemoname);
    }
}

我们可以使用Example表参数获取多行,这里我将多个行从scenario传递给java类。