Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom datatable transformer for cucumber-jvm

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

}
like image 979
pepuch Avatar asked Feb 05 '15 08:02

pepuch


1 Answers

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;
        }
    }
}
like image 161
M.P. Korstanje Avatar answered Oct 19 '22 12:10

M.P. Korstanje