Cucumber Scala provides an optional Default DataTable Transformer that uses Jackson.
It can be used to automatically convert DataTables to case classes without defining custom converters.
To use this optional transformer, you need to have Jackson Scala in your dependencies.
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-scala_2.13</artifactId>
<version>2.13.3</version>
<scope>test</scope>
</dependency>
Or:
libraryDependencies += "com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.13.3" % Test
The current version of Cucumber Scala has been tested against Jackson Module Scala version 2.13.3.
The transformer has to be added to your glue code by extending the JacksonDefaultDataTableEntryTransformer
trait.
For instance:
class MySteps extends ScalaDsl with EN with JacksonDefaultDataTableEntryTransformer {
// Your usual glue code
}
Note that it should be included only once in your glue code. If you use multiple glue classes, either add it to only one of them or add it to a separate object
.
The default empty string replacement used by the default transformer is [empty]
.
You can override it if you need to:
override def emptyStringReplacement: String = "[blank]"
Then, let the transformer do its work!
For instance, the following DataTable:
Given I have the following datatable
| field1 | field2 | field3 |
| 1.2 | true | abc |
| 2.3 | false | def |
| 3.4 | true | ghj |
will be automatically converted to the following case class:
case class MyCaseClass(field1: Double, field2: Boolean, field3: String)
Given("I have the following datatable") { (data: java.util.List[MyCaseClass]) =>
// Do something
}