Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Scala上不使用Cucumber从字符串转换为BigDecimal_Scala_Cucumber - Fatal编程技术网

在Scala上不使用Cucumber从字符串转换为BigDecimal

在Scala上不使用Cucumber从字符串转换为BigDecimal,scala,cucumber,Scala,Cucumber,我正在用Cucumber为Scala代码编写测试。我有以下步骤 When added product with price 10.0 以及以下步骤定义: When( """^added product with price ([\d\.]*)$""") { (price: BigDecimal) => { //something } } 从IntelliJ运行测试时,出现以下错误: cucumber.runtime.CucumberException: Don't

我正在用Cucumber为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)
我已经尝试实现我自己的转换器,但我无法添加scala.math.BigDecimal

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

您对Cucumber为什么不加载Cucumber.runtime.xstream.BigDecimalConverter有什么建议吗?

作为一种解决方法,我传递字符串并使用它创建BigDecimal

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

看起来不可能使用BigDecimal作为步骤的预期参数类型

经过一些研究,我找到了下一个可能的解决方案,如果目标是使用BigDecimal而不从字符串转换它(根据前面答案中提供的“变通方法”)

  • 使用自定义MyBigDecimal大小写类包装的BigDecimal创建Transformer类:

    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) {
    }
    
  • 然后可以在步骤定义中使用它:

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

  • 我对所有案例类都有这个问题,这真的令人失望。如果我拥有这个案例类,我可以提供我自己的转换器,但即使这样也很痛苦。