Custom data converter for cucumber-jvm

I want to create a custom datatable transformer in a cucumber. This is what my function looks like:

Given board
| o | _ | _ |
| o | _ | _ |
| o | _ | _ |

And I want to put it in a custom object. Let's say 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
}

The step definition works fine for the class DataTableandList<List<String>>

@Given("^board$")
public void board(DataTable board) throws Throwable {
    // this works fine
}

And it also works great

@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 to no avail. I also tried creating Transformer, but as I see it only works fine for strings (I want to use Datatable or List> in the input):

class BoardTransformer extends Transformer<Board> {

    @Override
    public Board transform(String value) {
        // TODO Auto-generated method stub
        return null;
    }

}
+7
source share
4 answers

If you can go with

@Given("^board$")
public void board(List<List<String>> board) throws Throwable {
...

Board

Board board2 = new Board(board);

, .

+1

cucumber 3.x TypeRegistryConfigurer Cucumber, Board DataTable. , TableTransformer TableCellTransformer, TableEntryTransformer TableRowTransformer.

TypeRegistryConfigurer .

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;
        }
    }
}
+1

, , " " " ", , , , - Jackson ObjectMapper ( de/serialization, Map to Object).

:

private ObjectMapper objectMapper = new ObjectMapper();

@When("I do some thing with input dataTable")
public void i_do_some_thing_with_input_dataTable(DataTable dataTable) {
    List<Map<String,String>> values = dataTable.asMaps(String.class, String.class);
    List<MyObject> inputs = values.stream().map(i -> objectMapper.convertValue(i,MyObject.class)).collect(Collectors.toList());

    inputs.forEach(i-> {
        //Do things with the input.
    });
}

/, , - , / .

:

  • MyObject ,
  • MyObject / .
0

cucumber, 1.2.2, , ( ) ,

@Given("^board$")
public void board(List<Board> board) throws Throwable {
    // process however you want
}

v3 , , XStream

0

All Articles