gpt4 book ai didi

Java - 小 cucumber 和 cucumber : Pass an object or list of objects on a vertical table instead of horizontal

转载 作者:搜寻专家 更新时间:2023-11-01 01:31:39 24 4
gpt4 key购买 nike

我的功能文件中有以下示例小 cucumber 场景:

Scenario: Book an FX Trade
Given trades with the following details are created:
|buyCcy |sellCcy |amount |date |
|EUR |USD |12345.67 |23-11-2017 |
|GBP |EUR |67890.12 |24-11-2017 |
When the trades are executed
Then the trades are confirmed

在我的胶水文件中,我可以将数据表映射到对象 Trade 作为开箱即用的 cucumber 解决方案:

@When("^trades with the following details are created:$")
public void trades_with_the_following_details_are_created(List<Trade> arg1) throws Throwable {
//do something with arg1
}

我想要实现的目标:
通过执行以下操作提高我的小 cucumber 场景的可读性:

  • 垂直转置数据表,如果我的对象有大约 10 个字段,这将提高可读性
  • 用别名替换字段/列名

    示例:

    Scenario: Book an FX Trade
    Given trades with the following details are created:
    |Buy Currency | EUR | GBP |
    |Sell Currency | USD | EUR |
    |Amount | 12345.67 | 67890.12 |
    |Date | 23-11-2017 | 24-11-2017 |
    When the trades are executed
    Then the trades are confirmed

    我希望表格是动态的,它可以包含多于或少于 2 个数据集/列。实现这一目标的最佳方法是什么?

    附加信息:
    语言:Java 8
    cucumber 版本:1.2.5

    Trade POJO 类似于:

    public class Trade {
    private String buyCcy;
    private String sellCcy;
    private String amount;
    private String date;

    /**
    * These fields are growing and may have around 10 or more....
    * private String tradeType;
    * private String company;
    */

    public Trade() {
    }

    /**
    * accessors here....
    */
    }
  • 最佳答案

    如果表在你的特征文件中指定为

    |buyCcy  | EUR        | GBP        |
    |sellCcy | USD | EUR |
    |amount | 12345.67 | 67890.12 |
    |date | 23-11-2017 | 24-11-2017 |

    您可以使用以下粘合代码(与您发布的 Trade 类一起使用,假设已实现适当的 toString() 方法)

    @Given("^trades with the following details are created:$")
    public void tradeWithTheFollowingDetailsAreCreated(DataTable dataTable) throws Exception {
    // transpose - transposes the table from the feature file
    // asList - creates a `List<Trade>`
    List<Trade> list = dataTable.transpose().asList(Trade.class);
    list.stream().forEach(System.out::println);
    }

    输出

    Trade{buyCcy=EUR, sellCcy=USD, amount=12345.67, date=23-11-2017}
    Trade{buyCcy=GBP, sellCcy=EUR, amount=67890.12, date=24-11-2017}

    关于Java - 小 cucumber 和 cucumber : Pass an object or list of objects on a vertical table instead of horizontal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47447023/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com