2014-06-27 50 views
4

我正在爲Cucumber編寫Scala代碼的測試。我有以下的步驟從字符串到BigDecimal的轉換不要在Scala上使用黃瓜

When added product with price 10.0 

而以下步驟定義:

When("""^added product with price ([\d\.]*)$""") { 
    (price: BigDecimal) => { 
    //something 
    } 
} 

當我的IntelliJ運行測試,我得到以下錯誤:

cucumber.runtime.CucumberException: Don't know how to convert "10.0" into scala.math.BigDecimal. 
Try writing your own converter: 

@cucumber.deps.com.thoughtworks.xstream.annotations.XStreamConverter(BigDecimalConverter.class) 
public class BigDecimal {} 

    at cucumber.runtime.ParameterInfo.convert(ParameterInfo.java:104) 
    at cucumber.runtime.StepDefinitionMatch.transformedArgs(StepDefinitionMatch.java:70) 
    at cucumber.runtime.StepDefinitionMatch.runStep(StepDefinitionMatch.java:38) 
    at cucumber.runtime.Runtime.runStep(Runtime.java:289) 
    at cucumber.runtime.model.StepContainer.runStep(StepContainer.java:44) 
    at cucumber.runtime.model.StepContainer.runSteps(StepContainer.java:39) 
    at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:40) 
    at cucumber.runtime.model.CucumberFeature.run(CucumberFeature.java:116) 
    at cucumber.runtime.Runtime.run(Runtime.java:120) 
    at cucumber.runtime.Runtime.run(Runtime.java:108) 
    at cucumber.api.cli.Main.run(Main.java:26) 
    at cucumber.api.cli.Main.main(Main.java:16) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) 

我試圖實現自己的變壓器,但我不能adnotate scala.math.BigDecimal

class BigDecimalConverter extends Transformer[BigDecimal] { 
    override def transform(p1: String): BigDecimal = BigDecimal(p1) 
} 

你有什麼建議爲什麼Cucumber沒有加載cucumber.runtime.xstream.BigDecimalConverter?

回答

0

作爲解決方法,我傳遞String並使用它創建BigDecimal。

When("""^added product with price ([\d\.]*)$""") { 
    (price: String) => { 
    something(BigDecimal(price)) 
    } 
} 
0

看起來不可能將BigDecimal用作Step的預期參數類型。

一些研究,我來下一個可能的解決方案後,如果目標是使用的BigDecimal不從字符串轉換它(按在前面的回答提供的「處理方法」。

  1. 創建Transformer類與包裹在定製MyBigDecimal情況下類的BigDecimal:

    import cucumber.api.{Transformer} 
    import cucumber.deps.com.thoughtworks.xstream.annotations.XStreamConverter 
    
    class MyBigDecimalTransformer extends Transformer[MyBigDecimal] { 
        override def transform(value: String): MyBigDecimal = { 
         MyBigDecimal(BigDecimal(value)) 
    }} 
    
    @XStreamConverter(classOf[MyBigDecimalTransformer]) 
    case class MyBigDecimal(value: BigDecimal) { 
    } 
    
  2. 則有可能在步驟定義來使用它:

    When("""^added product with price ([\d\.]*)$""") { 
    (price: MyBigDecimal) => { 
    //something 
    } 
    }