Skip to content

Add Cucumber Kotlin Junit5 example #2999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 30 commits into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
271b1a3
first commit
jit3pam May 16, 2025
8ca70be
add examples for Kotlin
jit3pam May 16, 2025
6311992
add examples for Kotlin date_calculator.feature
jit3pam May 16, 2025
b413ea6
Merge branch 'cucumber:main' into cucumber-kotlin-testng-examples
jit3pam May 16, 2025
2cbc758
add implementations for Kotlin date_calculator.feature
jit3pam May 16, 2025
27d061b
remove tag from RunCucumberTest.kt
jit3pam May 16, 2025
b3216e0
Merge remote-tracking branch 'origin/cucumber-kotlin-testng-examples'…
jit3pam May 16, 2025
5683424
Merge branch 'cucumber:main' into cucumber-kotlin-testng-examples
jit3pam May 17, 2025
785035c
Update examples/calculator-kotlin-testng/pom.xml
jit3pam May 19, 2025
326c3c6
restore pom.xml in examples
jit3pam May 19, 2025
698675f
update examples to use junit5
jit3pam May 21, 2025
50f08a4
Merge branch 'cucumber:main' into cucumber-kotlin-testng-examples
jit3pam May 21, 2025
194ab02
update dependencies in pom.xml
jit3pam May 21, 2025
7684f62
update dependencies in pom.xml, and change .* imports to explicit imp…
jit3pam May 21, 2025
ca7df3c
first commit
jit3pam May 16, 2025
0136a44
add examples for Kotlin
jit3pam May 16, 2025
b0db4b4
add examples for Kotlin date_calculator.feature
jit3pam May 16, 2025
7f46e5f
add implementations for Kotlin date_calculator.feature
jit3pam May 16, 2025
b954cfc
remove tag from RunCucumberTest.kt
jit3pam May 16, 2025
e6fba37
Update examples/calculator-kotlin-testng/pom.xml
jit3pam May 19, 2025
8125d05
restore pom.xml in examples
jit3pam May 19, 2025
8c1d5cf
update examples to use junit5
jit3pam May 21, 2025
450409c
update dependencies in pom.xml
jit3pam May 21, 2025
8390b15
update dependencies in pom.xml, and change .* imports to explicit imp…
jit3pam May 21, 2025
0a28103
restore the xml in examples
jit3pam May 21, 2025
6e8a56c
restore the xml in examples
jit3pam May 21, 2025
3478532
update the tags in examples/pom.xml
jit3pam May 21, 2025
6d15200
Merge remote-tracking branch 'origin/main' into cucumber-kotlin-testn…
mpkorstanje May 23, 2025
1f83c0b
[All] Set version to 7.23.0-SNAPSHOT
mpkorstanje May 23, 2025
b253d5e
Harmonize
mpkorstanje May 23, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/calculator-kotlin-testng/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.settings
/.classpath
/.project
/test-output/
86 changes: 86 additions & 0 deletions examples/calculator-kotlin-testng/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>examples</artifactId>
<groupId>io.cucumber</groupId>
<version>7.22.3-SNAPSHOT</version>
</parent>

<artifactId>calculator-kotlin-testng</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Kotlin Examples: Calculator - Annotations - TestNG</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.version>1.9.0</kotlin.version>
<kotlin.code.style>official</kotlin.code.style>
<junit.version>4.13.1</junit.version>
</properties>

<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test-junit</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>7.22.3-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>7.22.3-SNAPSHOT</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<testSourceDirectory>src/test/kotlin</testSourceDirectory>

<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.cucumber.examples.calaculator

import java.time.LocalDate
import java.util.*

class DateCalculator(private val now: LocalDate) {

fun isDateInThePast(date: LocalDate): String {
return if (date.isBefore(now)) "yes" else "no"
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.cucumber.examples.calaculator

class RpnCalculator {
private val stack: ArrayDeque<Number> = ArrayDeque()

private val OPS = setOf("+", "-", "*", "/")

fun push(arg: Any) {
if (arg in OPS) {
val y = stack.removeLast()
val x = if (stack.isEmpty()) 0 else stack.removeLast()
val valResult = when (arg) {
"-" -> x.toDouble() - y.toDouble()
"+" -> x.toDouble() + y.toDouble()
"*" -> x.toDouble() * y.toDouble()
"/" -> x.toDouble() / y.toDouble()
else -> throw IllegalArgumentException("Unknown operation $arg")
}
push(valResult)
} else {
stack.addLast(arg as Number)
}
}

fun peek(): Number? = stack.lastOrNull()

fun clear() = stack.clear()
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.cucumber.examples.calculator

import io.cucumber.examples.calaculator.DateCalculator
import io.cucumber.java.en.Given
import io.cucumber.java.en.Then
import io.cucumber.java.en.When
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.*
import kotlin.test.assertEquals

class DateStepDefinitions {

private lateinit var result: String

private lateinit var calculator: DateCalculator


@Given("today is {}")
fun todayIs(date: String) {
val date1 = LocalDate.parse(date)
calculator = DateCalculator(now = date1)
}

@When("I ask if {} is in the past")
fun iAskIfDateIsInPast(date: String) {
val formatter: DateTimeFormatter = DateTimeFormatter.ofPattern("MMM d, yyyy", Locale.ENGLISH)
val date2 = LocalDate.parse(date,formatter)
result = calculator.isDateInThePast(date2)
}

@Then("the result should be {string}")
fun theResultShouldBeYes(answer: String) {
assertEquals(answer, result)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.cucumber.examples.calculator

import io.cucumber.testng.AbstractTestNGCucumberTests
import io.cucumber.testng.CucumberOptions
import org.testng.annotations.DataProvider


@CucumberOptions(
plugin = ["html:target/results.html"]
)
class RunCucumberTest : AbstractTestNGCucumberTests() {

@DataProvider
override fun scenarios(): Array<Array<Any>> {
return super.scenarios()
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.cucumber.examples.calculator

import io.cucumber.datatable.DataTable
import io.cucumber.examples.calaculator.RpnCalculator
import io.cucumber.java.DataTableType
import io.cucumber.java.en.Given
import io.cucumber.java.en.Then
import io.cucumber.java.en.When
import kotlin.test.assertEquals

class ShoppingStepDefinitions {
private val rpnCalculator = RpnCalculator()
private val groceryList = mutableListOf<Grocery>()



@Given("the following groceries")
fun givenTheFollowingGroceries(grocery: DataTable){
val groceries: List<Grocery> = grocery.asList(Grocery::class.java)
groceryList.addAll(groceries)
for (gro in groceries){
rpnCalculator.push(gro.price.value)
rpnCalculator.push("+")
}
}

@When("I pay {int}")
fun whenIPay(amount: Int) {
rpnCalculator.push(amount)
rpnCalculator.push("-")
}

@Then("my change should be {int}")
fun myChangeShouldBe(change: Int) {
rpnCalculator.peek()?.let { assertEquals(change, -it.toInt()) }
}

@DataTableType
fun groceryEntry(entry: Map<String, String>): Grocery {
val name = entry["name"] ?: error("Missing name")
val price = Price.fromString(entry["price"] ?: error("Missing price"))
return Grocery(name, price)
}

data class Grocery(val name: String, val price: Price)

@JvmInline
value class Price(val value: Int) {
companion object {
fun fromString(value: String): Price {
return Price(value.toInt())
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@foo
Feature: Basic Arithmetic

Background: A Calculator
Given a calculator I just turned on

Scenario: Addition
# Try to change one of the values below to provoke a failure
When I add 4 and 5
Then the result is 9

Scenario: Another Addition
# Try to change one of the values below to provoke a failure
When I add 4 and 7
Then the result is 11

Scenario Outline: Many additions
Given the previous entries:
| first | second | operation |
| 1 | 1 | + |
| 2 | 1 | + |
When I press +
And I add <a> and <b>
And I press +
Then the result is <c>

Examples: Single digits
| a | b | c |
| 1 | 2 | 8 |
| 2 | 3 | 10 |

Examples: Double digits
| a | b | c |
| 10 | 20 | 35 |
| 20 | 30 | 55 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Feature: Dates with different date formats
This feature shows you can have different date formats, as long as you annotate the
corresponding step definition method accordingly.

@date
Scenario: Determine past date
Given today is 2011-01-20
When I ask if Jan 19, 2011 is in the past
Then the result should be "yes"
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Feature: Shopping

@shopping
Scenario: Give correct change
Given the following groceries
| name | price |
| milk | 9 |
| bread | 7 |
| soap | 5 |
When I pay 25
Then my change should be 4
Loading
Loading