Converting from String to BigDecimal with no cucumber on Scala

I am writing tests in Cucumber for Scala code. I have the next step

When added product with price 10.0

And the following step definition:

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

I get the following error when running a test from 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)

I tried to implement my own transformer, but I cannot adnotate scala.math.BigDecimal

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

Do you have any idea why Cucumber is not loading cucumber.runtime.xstream.BigDecimalConverter?

+4
source share
2 answers

As a workaround, I pass String and create a BigDecimal using it.

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

It appears that BigDecimal cannot be used as the expected parameter type for Step.

, BigDecimal String ( " ", .

  • Transformer BigDecimal, MyBigDecimal:

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

Source: https://habr.com/ru/post/1546268/


All Articles