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

时间:2016-03-28 07:55:18

标签: java cucumber cucumber-jvm

请考虑以下事项:

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

和功能:

Given this stuff:
  |Name|
  |Miguel|

最后,Scenario对象:

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;
  • ,它仍然是假的
  • ScenarioStuff(){ happy=true)
  • 的默认构造函数仍为false
  • 代码不会破坏该构造函数中的断点。

问题:

如果未在功能表中将参数作为参数提供,我如何默认happy=true

2 个答案:

答案 0 :(得分:1)

Cucumber使用XStream将步骤定义参数转换为Java对象,因此为了回答这个问题,我们必须深入研究XStream的方法。

this answer to an XStream question所述,一个选项是使用readResolve方法(XStream显然可以使用它来设置对象。

在我的特定情况下,将快乐变量从boolean更改为Boolean后,我最终得到了一个类似的实现:

ScenarioStuff{
private String Name;
private Boolean happy;

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

我还读到可以实现一个转换器来编组/解组该对象,但我没有探索这个选项1)它看起来不那么简单2)我没有立即看到如何注册这个新的转换器在黄瓜设置中。

答案 1 :(得分:0)

<强>更新 我添加了布尔值并让它们也起作用了。

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());
}

}

Class Price看起来像这样:

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;
    }
}