如何设置场景对象中属性的默认值(未调用构造函数!)

bf1o4zei  于 2021-07-13  发布在  Java
关注(0)|答案(2)|浏览(348)

考虑以下几点:

@Given("^this stuff:$")
public void this_stuff(List<ScenarioStuff> stuffList) throws Throwable {
    stuffList.get(0).isHappy();
}

其特点是:

Given this stuff:
  |Name|
  |Miguel|

最后,场景对象:

ScenarioStuff{
private String name;
private boolean happy;
(getters and setters for name and happy, inlcuding:)
public boolean isHappy(){
    return happy;
    }

以下是我的发现:
stufflist.get(0).ishappy();是假的;
即使 private boolean happy=true; 对于的默认构造函数,它仍为false ScenarioStuff(){ happy=true) 代码不会因该构造函数中的断点而中断。
问题:
如何默认 happy=true 如果在特征表中没有作为参数提供?

pw9qyyiw

pw9qyyiw1#

cucumber使用xstream将步骤定义参数转换为java对象,因此要回答这个问题,我们必须深入研究xstream的方法。
正如在回答xstream问题时所提到的,一个选项是使用readresolve方法(xstream显然使用这个方法来设置对象,如果可用的话)。
在我的特殊情况下,将happy变量从 booleanBoolean ,我最终实现了如下:

ScenarioStuff{
private String Name;
private Boolean happy;

private Object readResolve() {
        if(happy == null){
            happy = true;
        }
        return this;
  }
}

我还了解到可以实现一个转换器来封送/解封送对象,但我没有探讨这个选项,因为1)它似乎不那么简单,2)我没有立即看到如何在cucumber设置中注册这个新转换器。

gkn4icbw

gkn4icbw2#

更新我添加了布尔值,让它们也工作。

Scenario: An international coffee shop must handle currencies
Given the price list for an international coffee shop
  | product | currency | price | happy |
  | coffee  | EUR      | 1     | true  |
  | donut   | SEK      | 18    | false |
When I buy "1" "coffee" and "1" "donut"
Then I should pay "1" EUR be "happy" and "18" SEK be "unhappy"

步骤定义文件

public class datatable_steps {
    private HashMap<String, Price> intPriceList = new HashMap<String, Price>();
    private int sekSum;
    private int euroSum;

@Given("^the price list for an international coffee shop$")
public void the_price_list_for_an_international_coffee_shop(List<Price> prices) {

    int numPrices = prices.size();
    System.out.println("numPrices = " + numPrices);
    for(Price price : prices) {
        String key = price.getProduct();
        intPriceList.put(key,  price);
    }
}

@When("^I buy \"(\\d+)\" \"(.*)\" and \"(\\d+)\" \"(.*)\"$")
public void i_order_coffee_and_donut(int numberOfFirstItems, String firstItem,
                                     int numberOfSecondItems, String secondItem) throws Throwable {
    Price firstPrice = intPriceList.get(firstItem);
    calculate(numberOfFirstItems, firstPrice);
    Price secondPrice = intPriceList.get(secondItem);
    calculate(numberOfSecondItems, secondPrice);
}

private void calculate(int numberOfItems, Price price) {
    if (price.getCurrency().equals("SEK")) {
        sekSum += numberOfItems * price.getPrice();
        return;
    }
    if (price.getCurrency().equals("EUR")) {
        euroSum += numberOfItems * price.getPrice();
        return;
    }
    throw new IllegalArgumentException("The currency is unknown");
}

@Then("^I should pay \"(\\d+)\" EUR be \"(.*)\" and \"(\\d+)\" SEK be \"(.*)\"$")
public void should_I_pay_EUR_and_SEK(int expectedEuroSum, String eurHappy, int expectedSekSum, String sekHappy) throws Throwable {
    boolean eurHappyBool = false;
    boolean sekHappyBool = false;

    Assert.assertEquals(expectedEuroSum, euroSum);
    Assert.assertEquals(expectedSekSum,sekSum);

    if (eurHappy.equalsIgnoreCase("happy")) {
        eurHappyBool = true;
    }

    if (sekHappy.equalsIgnoreCase("happy")) {
        sekHappyBool = true;
    }

    Assert.assertEquals(eurHappyBool, intPriceList.get("coffee").isHappy());
    Assert.assertEquals(sekHappyBool, intPriceList.get("donut").isHappy());
}

}
类价格如下所示:

package runsupport;

public class Price {
    private String product;
    private String currency;
    private Integer price;
    private boolean happy;

    public Price(String product, Integer price, String currency){
        this.product = product;
        this.price = price;
        this.currency = currency;
    }

    public String getProduct() {
        return product;
    }

    public Integer getPrice() {
        return price;
    }

    public String getCurrency() {
        return currency;
    }

    public boolean isHappy() {
        return happy;
    }
}

相关问题