Есть несколько способов сделать это. Для # 2 и # 3 вам нужно добавить зависимость от jackson-databind
в ваш проект.
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)));
}
}
А в v5 вы бы сделали это следующим образом:
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));
}
}