I want to create custom datatable transformer in cucumber. This is how my feature looks like:
Given board
| o | _ | _ |
| o | _ | _ |
| o | _ | _ |
And I want to put it into custom object. Let's say that it looks like this:
class Board {
private List<List<String>> board;
public Board(List<List<String>> board) {
this.board = board;
}
}
My step definition should look like this:
@Given("^board$")
public void board(Board board) throws Throwable {
// todo
}
Step definition works fine for DataTable
class and List<List<String>>
@Given("^board$")
public void board(DataTable board) throws Throwable {
// this works fine
}
And this also works fine
@Given("^board$")
public void board(List<List<String>> board) throws Throwable {
// this also works fine
}
I tried to find a solution on the internet but without any success. I also tried to create Transformer
but, as I see, it works fine only for strings (I want to use Datatable or List> at the input):
class BoardTransformer extends Transformer<Board> {
@Override
public Board transform(String value) {
// TODO Auto-generated method stub
return null;
}
}
In cucumber 3.x you can use the TypeRegistryConfigurer
to inform Cucumber how it should create Board
objects from DataTable
. Because you want to transform the whole table to a single object you have to use a TableTransformer
rather then TableCellTransformer
,TableEntryTransformer
or TableRowTransformer
.
You can place the TypeRegistryConfigurer
anywhere on the glue path.
package io.cucumber.java.test;
import io.cucumber.core.api.TypeRegistryConfigurer;
import io.cucumber.core.api.TypeRegistry;
import io.cucumber.datatable.DataTableType;
import io.cucumber.datatable.TableTransformer;
import java.util.List;
import java.util.Locale;
import static java.util.Locale.ENGLISH;
public class TypeRegistryConfiguration implements TypeRegistryConfigurer {
@Override
public Locale locale() {
return ENGLISH;
}
@Override
public void configureTypeRegistry(TypeRegistry typeRegistry) {
typeRegistry.defineDataTableType(new DataTableType(
Board.class,
(TableTransformer<Board>) table -> new Board(table.cells())));
}
static class Board {
private final List<List<String>> board;
private Board(List<List<String>> board) {
this.board = board;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With