5

I have the following sample gherkin scenario on my feature file:

Scenario: Book an FX Trade
 Given trades with the following details are created:
   |buyCcy |sellCcy |amount   |date       |
   |EUR    |USD     |12345.67 |23-11-2017 |
   |GBP    |EUR     |67890.12 |24-11-2017 |
 When the trades are executed
 Then the trades are confirmed

In my glue file, I can map the data table to an object Trade as an out of the box cucumber solution:

@When("^trades with the following details are created:$")
public void trades_with_the_following_details_are_created(List<Trade> arg1) throws Throwable {
        //do something with arg1
}

What I want to achieve:
Improve the readability of my gherkin scenario by doing the following:

  • Transpose the data table vertically, This will improve readability if my object has around 10 fields
  • Replace fields / column names with aliases

    Sample:

    Scenario: Book an FX Trade
     Given trades with the following details are created:
       |Buy Currency  | EUR        | GBP        |
       |Sell Currency | USD        | EUR        |
       |Amount        | 12345.67   | 67890.12   |
       |Date          | 23-11-2017 | 24-11-2017 |
     When the trades are executed
     Then the trades are confirmed
    

    I want the table to be dynamic in a way that it can have more or less than 2 data sets / columns. What would be the best way to achieve this?

    Additional info:
    Language: Java 8
    Cucumber version: 1.2.5

    Trade POJO being something like:

    public class Trade {
        private String buyCcy;
        private String sellCcy;
        private String amount;
        private String date;
    
        /**
         * These fields are growing and may have around 10 or more....
         * private String tradeType;
         * private String company;
         */
    
        public Trade() {
        }
    
        /**
         * accessors here....
         */
    }
    
  • iamkenos
    • 1,446
    • 8
    • 24
    • 49

    1 Answers1

    11

    If the table is specified in your feature file as

    |buyCcy  | EUR        | GBP        |
    |sellCcy | USD        | EUR        |
    |amount  | 12345.67   | 67890.12   |
    |date    | 23-11-2017 | 24-11-2017 |
    

    you can use the following glue code (with your posted Trade class, assuming that there is a proper toString() method implemented)

    @Given("^trades with the following details are created:$")
    public void tradeWithTheFollowingDetailsAreCreated(DataTable dataTable) throws Exception {
        // transpose - transposes the table from the feature file
        // asList - creates a `List<Trade>`
        List<Trade> list = dataTable.transpose().asList(Trade.class);
        list.stream().forEach(System.out::println);
    }
    

    output

    Trade{buyCcy=EUR, sellCcy=USD, amount=12345.67, date=23-11-2017}
    Trade{buyCcy=GBP, sellCcy=EUR, amount=67890.12, date=24-11-2017}
    
    SubOptimal
    • 22,518
    • 3
    • 53
    • 69
    • 2
      plus 1 for the effort, but I simply *hate* cucumber tests and their way to set them up... – Eugene Nov 23 '17 at 11:41
    • 1
      @Eugene Out of curiosity. What do you propose instead of [Cucumber](https://cucumber.io/docs/reference/jvm#java) for [BDD](https://en.wikipedia.org/wiki/Behavior-driven_development)? – SubOptimal Nov 23 '17 at 12:32
    • 1
      that's a very good question... next question? seriously I don't know; all I can tell that there is zero pleasure involved when I have to do these tests (hey I even re-fused workplaces where I *knew* I had to do this)... especially when these tests get *big* - you probably understand what I mean – Eugene Nov 23 '17 at 12:34
    • Never knew it was this simple. I feel a bit ashamed I didn't check DataTable methods first XD. Thanks!! – iamkenos Nov 24 '17 at 03:51