Use Cucumber with Kotlin
maincucumber-kotlin-java8 module is specifically designed for running tests. For standard Kotlin development with Cucumber, you should use cucumber-java or cucumber-java8 directly in your Kotlin project.repository·main·Indexed 25 days ago
https://github.com/cucumber/cucumber-jvmJava implementation of Cucumber for running automated tests written in Gherkin. Includes guides on using the cucumber-archetype for project setup, managing dependencies with cucumber-bom, and implementing step definitions with cucumber-java. Provides detailed documentation on dependency injection via cucumber-guice and cucumber-jakarta-cdi, as well as usage of hooks (@Before, @After, @BeforeAll, @AfterAll), Data Tables, Doc Strings, and custom @ParameterType and @DataTableType definitions.
cucumber-kotlin-java8 module is specifically designed for running tests. For standard Kotlin development with Cucumber, you should use cucumber-java or cucumber-java8 directly in your Kotlin project.To use Hamcrest matchers for comparing Cucumber DataTables, add the datatable-matchers dependency to your pom.xml. It is recommended to use the cucumber-bom for dependency management to ensure version compatibility.
<dependencies>
[...]
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>datatable-matchers</artifactId>
<scope>test</scope>
</dependency>
[...]
</dependencies>The Cucumber runner supports JUnit's Assume (JUnit 4) and Assumptions (JUnit 5) utility methods.
Unlike failed assertions which result in a test failure, a failed assumption results in the test being aborted. Use this when a test depends on a condition that might not exist in the current environment (e.g., a specific file or service). When an assumption fails, Cucumber will mark the scenario as skipped.
Cucumber JVM allows you to run automated tests written in plain language on the JVM. To begin using it, you can follow the official installation guides, consult the documentation, or use starter projects for Maven and Gradle.
By default, Cucumber interprets empty cells in Gherkin data tables as null.
To represent empty strings, you can configure a replacement string (e.g., [blank]) using the replaceWithEmptyString argument in DataTableType, DefaultDataTableCellTransformer, or DefaultDataTableEntryTransformer.
package com.example.app;
import io.cucumber.datatable.DataTable;
import io.cucumber.java8.En;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
class StepDefinitions implements En {
StepDefinitions() {
// Configure '[blank]' to be treated as an empty string replacement
DataTableType("[blank]", (Map<String, String> entry) -> new Author(
entry.get("name"),
entry.get("first publication")
));
Given("some authors", (DataTable authorsTable) -> {
// authors = [Author(name="Aspiring Author", firstPublication=null), Author(name="Ancient Author", firstPublication="")]
});
}
}To use lambda-based step definitions, add the cucumber-java8 dependency to your pom.xml. It is recommended to use cucumber-bom for dependency management.
<dependencies>
[...]
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java8</artifactId>
<scope>test</scope>
</dependency>
[...]
</dependencies>To ensure all Cucumber dependencies are synchronized to the same version within a Maven project, use the cucumber-bom. By importing the BOM in your <dependencyManagement> section, you can declare Cucumber dependencies (like cucumber-java or cucumber-junit) without specifying their versions explicitly.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-bom</artifactId>
<version>${cucumber.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- Then for example -->
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>To avoid writing manual transformers for every type, you can implement TableEntryByTypeTransformer and TableCellByTypeTransformer. This allows you to use an object mapper like Jackson to automatically convert cells and entries to any registered type.
Note: If both are installed, Cucumber defaults to assuming table entries over table cells. You can resolve this ambiguity by ensuring your tables include a header row.
private class JacksonDataTableTransformer implements TableEntryByTypeTransformer, TableCellByTypeTransformer {
ObjectMapper objectMapper = new tools.jackson.databind.ObjectMapper();
@Override
public <T> T transform(String value, Class<T> cellType) {
return objectMapper.convertValue(value, cellType);
}
@Override
public <T> T transform(Map<String, String> entry, Class<T> type, TableCellByTypeTransformer cellTransformer) {
return objectMapper.convertValue(entry, type);
}
}To use CDI Standalone Edition (CDI SE) API for dependency injection in your step definitions, add the cucumber-jakarta-cdi dependency to your pom.xml. It is recommended to use the cucumber-bom for dependency management.
IMPORTANT: This module uses the jakarta flavor of CDI, not the javax flavor.
<dependencies>
[...]
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-jakarta-cdi</artifactId>
<scope>test</scope>
</dependency>
[...]
</dependencies>To use Google Guice dependency injection in your Cucumber tests, add the cucumber-guice dependency to your pom.xml. It is recommended to use the cucumber-bom for dependency management.
<dependencies>
[...]
dependency
<groupId>io.cucumber</groupId>
<artifactId>cucumber-guice</artifactId>
<scope>test</scope>
</dependency>
[...]
</dependencies><dependencies>
[...]
dependency
<groupId>io.cucumber</groupId>
<artifactId>cucumber-guice</artifactId>
<scope>test</scope>
</dependency>
[...]
</dependencies>AbstractTestNGCucumberTests, you can use the TestNGCucumberRunner class to compose your tests manually.