2016-03-28 12 views
0

は、次のことを考えてみましょう:(!コンストラクタが呼び出されていない)

@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;
  • それは、そのコンストラクタにブレークポイントで中断されませんScenarioStuff(){ happy=true)
  • コードのデフォルトコンストラクタで偽残っていても、偽のまま。

質問:

それがフィーチャーテーブルのパラメータとして提供されていない場合はどのようにhappy=trueをデフォルトすることができますか?

+0

isHappy()メソッドです。 ScenarioStuffのクラス定義は何ですか?あなたの機能はシナリオの輪郭のように見えますが、はどこにも表示されません。 [Doc String](https://cucumber.io/docs/reference#doc-strings)を使用することを意味しましたか?質問を明確にしてください。 – MikeJRamsey56

+0

isHappyは幸せのゲッターです(更新されたコードを参照)。フィーチャー・テーブルに名前が表示されます(「物を考える」を参照)。私は大文字のテキストではなくテーブルを渡すので、ここでdoc文字列が当てはまるとは思わない。 – mmalmeida

+0

「Miguel」という文字列をList stuffListレシーバに渡しています。おそらくうまくいくわけではないでしょう。文字列を文字列として受け入れ、ステップボディに明示的にScenarioStuffを作成するのはなぜですか? – MikeJRamsey56

答えて

1

キュウリは、ステップ定義の引数をJavaオブジェクトに変換するためにXStreamを使用しています。これに答えるには、これを行うためにXStreamの方法を掘り下げなければなりません。私の特定のケースで

this answer to an XStream questionに述べたように、一つの選択肢が利用可能な場合XStreamのは、明らかにオブジェクトを設定するには、使用するreadResolveメソッドを(使用することです。

booleanからBooleanに幸せな変数を変更した後、私のような実装になってしまった:

私もそれはあまり簡単なように見えた)1は、オブジェクトアンマーシャリング/マーシャリングするコンバータを実装することができますが、私は1のように、このオプションを探求しなかったことを読んで
ScenarioStuff{ 
private String Name; 
private Boolean happy; 

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

及び2)Iこの新しいコンバータウィットをどのように登録するのかはすぐには分かりませんでしたキュウリのセットアップのヒント。

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

}

クラスの価格は次のようになります。

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

@mmalmeida幸いなことに、Priceクラスのコンストラクタを更新しなかったことに注目してください。出来た。セッターはない、それは興味深い。 – MikeJRamsey56

+0

これは、XStreamが注入を行っているからです(私の答えを見てください)。オリジナルの投稿と同じシナリオ(1)フィーチャのハッピーコラムを削除し、(2)the_price_list_for_an_international_coffee_shopメソッド内で満足している方法を見つけます。 – mmalmeida

+0

@ mmalmeida実際の質問は、受信側のステップ定義の引数リストに指定されたクラスのインスタンス変数は、機能のデータテーブルに値が与えられていないのですか?そして、どのように幸せは本当の価値を得ましたか? – MikeJRamsey56

関連する問題