cucumber.api
Class Transformer<T>

java.lang.Object
  extended by cucumber.api.Transformer<T>
Type Parameters:
T - the type to be instantiated
All Implemented Interfaces:
cucumber.deps.com.thoughtworks.xstream.converters.ConverterMatcher, cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter

public 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 I did my laundry 3 days ago

Let's assume we want Cucumber to transform the substring "3 days ago" into an instance of our custom HumanTime class:

     @Given("I did my laundry (.*)")
     public void iDidMyLaundry(HumanTime t) {
     }
 

If the HumanTime class has a constructor with a single String argument, then Cucumber will instantiate it without any further ado. However, if the class you want to convert to does not have a String constructor you can annotate your parameter:

     @Given("I did my laundry (.*)")
     public void iDidMyLaundry(@Transform(HumanTimeConverter.class) HumanTime t) {
     }
 

And then a HumanTimeConverter class:

public static class HumanTimeConverter extends Transformer<HumanTime> {
         &#064;Override
         public HumanTime transform(String value) {
             // Parse the value here, and create a new instance.
             return new HumanTime(...);
         }
     }
 

An alternative to annotating parameters with Transform is to annotate your class with XStreamConverter:

     @XStreamConverter(HumanTimeConverter.class)
     public class HumanTime {
     }
 

This will also enable a DataTable to be transformed to a List<YourClass;>

See Also:
Transform

Constructor Summary
Transformer()
           
 
Method Summary
 boolean canConvert(Class type)
           
 Object fromString(String s)
           
 String toString(Object o)
           
abstract  T transform(String value)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Transformer

public Transformer()
Method Detail

toString

public String toString(Object o)
Specified by:
toString in interface cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter

fromString

public Object fromString(String s)
Specified by:
fromString in interface cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter

canConvert

public boolean canConvert(Class type)
Specified by:
canConvert in interface cucumber.deps.com.thoughtworks.xstream.converters.ConverterMatcher

transform

public abstract T transform(String value)


Copyright © 2012. All Rights Reserved.