We use Selectors that return a list of objects all over the application and of course we want to use them in our Fitnesse tests.

To be able to do so, we introduced a new kind of Fixture that uses reflection to fill a map – one entry for each result of the selector. Basically, all we did was expand Fitnesse’s RowFixture and override the query() Method:

  1. @Override
  2. public Object[] query() throws Exception {
  3.    final List<Map<String, Object>> result =
  4.             new LinkedList<Map<String, Object>>();
  5.    for (ValueObject each : executeSelector()) {
  6.        Map<String, Object> row = new HashMap<String, Object>();
  7.        for (Field field : each.getClass().getFields()) {
  8.           if (Modifier.isStatic(field.getModifiers()) && field.getType().equals(String.class)) {
  9.              row.put(field.getName(), each.getAttributeAllowingNull((String) field.get(null)));
  10.           }
  11.       }
  12.   }
  13.   return result.toArray(new Object[v.size()]);
  14. }

executeSelector() is abstract and has to be overwritten in each subclass of this Fixture to return whatever is needed. In Fitnesse, you can directly use the the field names:

|User Selector|
|NAME|AGE|GENDER|
|Daisy|32|female|
|Donald|34|male|
|Minnie|25|female|

That way we are able to write slim fixtures and don’t have to waste time writing getters for each field as we did before.

Email this Share this on Facebook Share this on LinkedIn Tweet This! RSS feed for comments on this post. TrackBack URL

Leave a comment