T
- the type to be instantiatedpublic abstract class Transformer<T> extends Object implements cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter
Allows transformation of a step definition argument to a custom type, giving you full control over how that type is instantiated.
Consider the following Gherkin step:
Given today's date is "10/03/1985"
As an example, let's assume we want Cucumber to transform the substring "10/03/1985" into an instance of
org.joda.time.LocalDate
class:
@Given("today's date is \"(.*)\"") public void todays_date_is(LocalDate d) { }
If the parameter's class has a constructor with a single String
or Object
argument, then
Cucumber will instantiate it without any further ado. However, in this case that might not give you what you
want. Depending on your Locale, the date may be Oct 3 or March 10!
This is when you can use a custom transformer. You'll also have to do that if your parameter class doesn't
have a constructor with a single String
or Object
argument. For the JODA Time
example:
@Given("today's date is \"(.*)\"") public void todays_date_is(@Transform(JodaTimeConverter.class) LocalDate d) { }
And then a JodaTimeConverter
class:
public static class JodaTimeConverter extends Transformer<LocalDate> {
private static DateTimeFormatter FORMATTER = DateTimeFormat.forStyle("S-");
@Override
public LocalDate transform(String value) {
return FORMATTER.withLocale(getLocale()).parseLocalDate(value);
}
}
An alternative to annotating parameters with Transform
is to annotate your class with
XStreamConverter
:
@XStreamConverter(MyConverter.class) public class MyClass { }
This will also enable a DataTable
to be transformed to
a List<MyClass;>
Transform
Constructor and Description |
---|
Transformer() |
Modifier and Type | Method and Description |
---|---|
boolean |
canConvert(Class type) |
Object |
fromString(String s) |
protected Locale |
getLocale() |
void |
setParameterInfoAndLocale(cucumber.runtime.ParameterInfo parameterInfo,
Locale locale) |
String |
toString(Object o) |
abstract T |
transform(String value) |
public String toString(Object o)
toString
in interface cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter
public final Object fromString(String s)
fromString
in interface cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter
public boolean canConvert(Class type)
canConvert
in interface cucumber.deps.com.thoughtworks.xstream.converters.ConverterMatcher
public void setParameterInfoAndLocale(cucumber.runtime.ParameterInfo parameterInfo, Locale locale)
protected Locale getLocale()
Copyright © 2017. All rights reserved.