Dienstag, 25. Juli 2017

Using Java 8 Features in Cucumber SpecDefinitions

with cucumber java 8 function you have a nice way to use anonymous function for your feature files which makes the old annotations and underscore naming convention obsolete Example code for the ultimate Bowling game .Feature from the Bowling game Kata
  Scenario: Calculating Game Score for utimate Bowling game
    Given I play a 10 rounds Bowling game
    When I bowl a game X X X X X X X X XX X
    Then the game result will be 300 points

Scenario: Calculating Game Score for the worst Bowling game
    Given I play a 10 rounds Bowling game
    When I bowl a game 00 00 00 00 00 00 00 00 00 00
    Then the game result will be 0 points

package bowling;

import cucumber.api.java8.En;
import org.junit.Assert;

public class GameStepDefs implements En {
    private Game game;
    public GameStepDefs() {
        Given("^I play a (\\d+) rounds Bowling game$", (Integer arg0) -> {
            this.game = new Game(10);
        });
        When("^I bowl a game ([0-9, X//]+)$", (String gameString) -> {
            this.game = GameStringReader.createGameFromString(gameString);
        });
        Then("^the game result will be (\\d+) points$", (Integer gameResult) -> {
            Assert.assertEquals(gameResult, this.game.getPoints());
        });

    }
}

Make sure to add the cucumber-java8 plugin to your dependencies in Gradle or other build Tool
dependencies {
    testCompile 'info.cukes:cucumber-java:' + cucumberVersion
    testCompile 'info.cukes:cucumber-java8:' + cucumberVersion
    testCompile 'info.cukes:cucumber-junit:' + cucumberVersion
    testCompile 'junit:junit:' + junitVersion
}

Keine Kommentare:

Kommentar veröffentlichen