|
| 1 | +# Default Jackson DataTable Transformer |
| 2 | + |
| 3 | +Cucumber Scala provides an optional Default DataTable Transformer that uses Jackson. |
| 4 | + |
| 5 | +It can be used to automatically convert DataTables to case classes without defining custom converters. |
| 6 | + |
| 7 | +## Add Jackson dependency |
| 8 | + |
| 9 | +To use this optional transformer, you need to have Jackson Scala in your dependencies. |
| 10 | + |
| 11 | +```xml |
| 12 | +<dependency> |
| 13 | + <groupId>com.fasterxml.jackson.module</groupId> |
| 14 | + <artifactId>jackson-module-scala_2.13</artifactId> |
| 15 | + <version>2.10.3</version> |
| 16 | + <scope>test</scope> |
| 17 | +</dependency> |
| 18 | +``` |
| 19 | + |
| 20 | +Or: |
| 21 | +```sbt |
| 22 | +libraryDependencies += "com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.10.3" % Test |
| 23 | +``` |
| 24 | + |
| 25 | + |
| 26 | +The current version of Cucumber Scala has been tested against Jackson Module Scala **version 2.10.3**. |
| 27 | + |
| 28 | +## Add the transformer |
| 29 | + |
| 30 | +The transformer has to be added to your glue code by extending the `JacksonDefaultDataTableEntryTransformer` trait. |
| 31 | + |
| 32 | +For instance: |
| 33 | +```scala |
| 34 | +class MySteps extends ScalaDsl with EN with JacksonDefaultDataTableEntryTransformer { |
| 35 | + // Your usual glue code |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +### Empty string replacement |
| 40 | + |
| 41 | +The default empty string replacement used by the default transformer is `[empty]`. |
| 42 | + |
| 43 | +You can override it if you need to: |
| 44 | +```scala |
| 45 | +override def emptyStringReplacement: String = "[blank]" |
| 46 | +``` |
| 47 | + |
| 48 | +## Example |
| 49 | + |
| 50 | +Then, let the transformer do its work! |
| 51 | + |
| 52 | +For instance, the following DataTable: |
| 53 | +```gherkin |
| 54 | +Given I have the following datatable |
| 55 | +| field1 | field2 | field3 | |
| 56 | +| 1.2 | true | abc | |
| 57 | +| 2.3 | false | def | |
| 58 | +| 3.4 | true | ghj | |
| 59 | +``` |
| 60 | + |
| 61 | +will be automatically converted to the following case class: |
| 62 | +```scala |
| 63 | +case class MyCaseClass(field1: Double, field2: Boolean, field3: String) |
| 64 | + |
| 65 | +Given("I have the following datatable") { (data: java.util.List[MyCaseClass]) => |
| 66 | + // Do something |
| 67 | +} |
| 68 | +``` |
0 commit comments