This article was first published on Medium.
My team is creating an application in Kotlin. To make development of Cucumber tests easier, we decided to also use Cucumber-jvm with Kotlin instead of Java. Fortunately, it is possible to use cucumber-java8 in Kotlin (kotlin-java8)
Prerequisites
If you’d like to follow along, make sure you have the following installed:
- JDK, which you can get from Oracle: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
- An IDE, for instance IntelliJ IDEA (https://www.jetbrains.com/idea/download/#section=mac), which I use here.
- Maven, which you can get here: https://maven.apache.org/
Add dependencies
We use Maven, so we added the following dependencies to our pom.xml:
Note: The cucumber-junit dependency is added so we can add a JUnit Runner to run our tests, which we will do later.
If you don’t have Kotlin already configured in your project, you’ll need to add those dependencies also (or have IntelliJ IDEA do it for you).
Add a feature file
In our src/test/resources folder we create a new directory and add a .feature file. For this example, we’ll reuse the belly.feature from the cucumber-java-skeleton
Unfortunately the IntelliJ IDEA Cucumber plugin does not (yet) support a link between the steps in our feature file and our step definitions, like it does for Java. Also, we cannot generate snippets in Kotlin directly. Fortunately there is a work around: we can run the tests to generate snippets in Java 8 lambda style.
You can run the test from IntelliJ IDEA by right-clicking the feature file and selecting “Run ‘Feature:belly’” from the context menu.
When you run the tests, you should get something like the following result:
Add Step Definitions
In the src/test/kotlin folder, we add a new Kotlin File/Class, called `StepDefs`.
We only have to write a little Kotlin code in the file:
Note that our StepDefs implement the `cucumber.api.java8.En` interface, so we need to import it.
Now, when we copy-paste the generated snippets inside the `init{}` block, IntelliJ IDEA offers to convert it to Kotlin for us. Once we do, we will also need to import the `cucumber.api.PendingException` mentioned in the snippets.
Now we have the following StepDefs.kt file and we can start implementing the steps, as well as the code to make them pass!
To run our features from a JUnit runner, we’ll need to add one. In the src/test/kotlin folder, we add a new Kotlin File/Class, called RunKukesTest.
This code is available on GitHub: https://github.com/mlvandijk/kukumber-skeleton