Reputation: 337
For cucumber 3.0 I was using
typeRegistry.defineDataTableType(DataTableType.entry(CustomData.class));
public class CustomData {
private int id;
private int val;
private Region region;
private Boolean isExisting;
private String type;
//getter and setter methods
}
How to convert this in cucumber 4.0.0 as part of configureTypeRegistry
My step in feature file as
When I set the custom data
| region | id | val | isExisting | type |
| NA | 2 | 10 | true | custom|
Upvotes: 0
Views: 1981
Reputation: 12029
There are a few ways to do it. For #2 and #3 you'll have to add a dependency on jackson-databind
to your project.
import com.fasterxml.jackson.databind.ObjectMapper;
import io.cucumber.core.api.TypeRegistry;
import io.cucumber.core.api.TypeRegistryConfigurer;
import io.cucumber.datatable.DataTableType;
import java.util.Map;
class TypeRegistryConfiguration implements TypeRegistryConfigurer {
private final ObjectMapper objectMapper = new ObjectMapper();
@Override
public void configureTypeRegistry(TypeRegistry typeRegistry) {
// 1. Define the mapping yourself.
typeRegistry.defineDataTableType(
new DataTableType(MyType.class,
(Map<String, String> entry) -> {
MyType object = new MyType();
object.setX(entry.get("X"));
return object;
}
)
);
// 2. Define a data table type that delegates to an object mapper
typeRegistry.defineDataTableType(
new DataTableType(MyType.class,
(Map<String, String> entry) -> objectMapper.convertValue(entry, MyType.class)
)
);
// 3. Define a default data table entry that takes care of all mappings
typeRegistry.setDefaultDataTableEntryTransformer(
(entryValue, toValueType, cellTransformer) ->
objectMapper.convertValue(entryValue, objectMapper.constructType(toValueType)));
}
}
And in v5 you would do this like:
import com.fasterxml.jackson.databind.ObjectMapper;
import io.cucumber.java.DataTableType;
import io.cucumber.java.DefaultDataTableEntryTransformer;
import java.lang.reflect.Type;
import java.util.Map;
class TypeRegistryConfiguration {
private final ObjectMapper objectMapper = new ObjectMapper();
// 1. Define the mapping yourself
@DataTableType
public MyType myType(Map<String, String> entry) {
MyType object = new MyType();
object.setX(entry.get("X"));
return object;
}
// 2. Define a data table type that delegates to an object mapper
@DataTableType
public MyType myType(Map<String, String> entry) {
return objectMapper.convertValue(entry, MyType.class);
}
// 3. Define a default data table entry that takes care of all mappings
@DefaultDataTableEntryTransformer
public Object defaultDataTableEntry(Map<String, String> entry, Type toValueType) {
return objectMapper.convertValue(entry, objectMapper.constructType(toValueType));
}
}
Upvotes: 5